Solace
Solace

Reputation: 9020

Simple Android client for consuming REST webservice using Retrofit - why am I getting 'Failed to connect to server' error

I wrote a little Restful webservice in PHP (code given at the end of question) and it is in my local XAMPP server. When I access it in the browser using http://localhost/Test8/?name=Java as URL, I get the following data as expected.

{"status":200,"status_message":"Book found","data":999}

This means the webservice works correctly.

Then I went ahead to write an Android app following this tutorial, to consume this webservice using the Retrofit library. My code is given as follows.

The problem is that the output I get is Failed to connect to localhost/###.#.#.#:## where ###.#.#.#:## is my IP address.

enter image description here

MainActivity.java:

package tests.retrofitusageone;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.mainActivity_textView);

        String url = "http://localhost/Test8/index.php?name=Java";
        //String url ="https://graph.facebook.com/youtube?fields=id,name,likes&access_token=1541785476124013|sWrV2Qgw_knjFiQheJ_iT5uir84";

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(url).build();

        RestInterface restInterface = restAdapter.create(RestInterface.class);

        restInterface.get_price(new Callback<RestJsonPojo>() {

            @Override
            public void success(RestJsonPojo restJsonPojo, Response response) {
                textView.setText("Price of the book: " + restJsonPojo.getData());
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                String retrofitErrorString = retrofitError.getMessage();
                textView.setText(retrofitErrorString);
            }

        });
    }
}

RestJsonPojo.java:

package tests.retrofitusageone;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class RestJsonPojo {
    @SerializedName("status")
    @Expose
    private Integer status;
    @SerializedName("status_message")
    @Expose
    private String statusMessage;
    @SerializedName("data")
    @Expose
    private Integer data;

    /**
     * 
     * @return The status
     */
    public Integer getStatus() {
        return status;
    }

    /**
     * 
     * @param status
     *            The status
     */
    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * 
     * @return The statusMessage
     */
    public String getStatusMessage() {
        return statusMessage;
    }

    /**
     * 
     * @param statusMessage
     *            The status_message
     */
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    /**
     * 
     * @return The data
     */
    public Integer getData() {
        return data;
    }

    /**
     * 
     * @param data
     *            The data
     */
    public void setData(Integer data) {
        this.data = data;
    }
}

RestInterface.java:

package tests.retrofitusageone;

import retrofit.Callback;
import retrofit.http.GET;

interface RestInterface {

    //@GET("/youtube?fields=id,name,likes&access_token=1541785476124013|sWrV2Qgw_knjFiQheJ_iT5uir84")
    @GET("/index.php?name=Java")
    void get_price(Callback<RestJsonPojo>callback);
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tests.retrofitusageone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainActivity_textView" />"

</RelativeLayout>

localhost/Test8/index.php:

<?php

//Process client's request (via URL)
header("Content-Type:application/json");

if (  !empty($_GET['name'])  ) {
    $name = $_GET['name'];
    $price = get_price($name);

    if (empty($price)) {
        //Book not found
        deliver_response(200, 'Book not found!', NULL);
    } else {
        //Send the response with book price
        deliver_response(200, 'Book found', $price);
    }

} else {
    //throw invalid request
    deliver_response(400, "Invalid Request", NULL);
}



 //API Functions
 function get_price($bookRequested) {
     $books = array(
        'Java' => 999,
        'C' => 348,
        'PHP' =>500
     );

     foreach ($books as $book=>$price) {
         if ($book == $bookRequested) {
             return $price;
         }
     }
 }


 function deliver_response($status, $status_message, $data) {
     header("HTTP/1.1 $status $status_message");

     $response['status'] = $status;
     $response['status_message'] = $status_message;
     $response['data'] = $data;

     $json_response = json_encode($response);

     echo $json_response;
 }

?>

Upvotes: 0

Views: 1055

Answers (2)

Pratik Jambhulkar
Pratik Jambhulkar

Reputation: 69

I guess the problem is in MainActivity where you are providing the server url.

Try changing the url to

 String url = "http://localhost/Test8/";

Since, you have already written it in the RestInterface interface.

While initialising the restadapter, always use just the base url and the endpoints in the interface class.

Upvotes: 0

user4436713
user4436713

Reputation:

From your android device you should not put localhost/###.#.#.#:##, but instead just your server address and port like ###.#.#.#:##, for example 192.168.1.12:80. Because you are not running any server on your android app.

Upvotes: 2

Related Questions