Arihant Arihant - 1 year ago 92
Android Question

Android web service not parsing data from localhost

I am trying to connect Android App to java webservice to get pHp parsed values.

Php Script is :

$data = array('name' => 'Froyo', 'version' => 'Android 2.2');
print (json_encode($data));

And the Java WebService is :

package com.json.php;

import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONExampleActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("");
TextView textView = (TextView)findViewById(;
try {

HttpResponse response = httpclient.execute(httppost);
String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
JSONObject object = new JSONObject(jsonResult);

String name = object.getString("name");
String verion = object.getString("version");
textView.setText(name + " - " + verion);

catch (JSONException e) {
catch (ClientProtocolException e) {
catch (IOException e) {

private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {
while ((rLine = rd.readLine()) != null) {

catch (IOException e) {
return answer;

I tried both and http://localhost/test.php
in HttpPost, It doesn't works.
I checked my php file on the browser and that gives the result fine.
Where am i Going wrong?
Also i am working on a Mac, is that anyway related to the problem because of all the permission issues?

Answer Source

Just take a look at this page :

You'll notice that they refer to:

  • as Special alias to your host loopback interface (i.e., on your development machine)
  • as The emulated device's own loopback interface

Hope I answered and solved your problem :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download