2
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpProtocolParams;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    private final String TAG = "ACCESS";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method.equals("POST")){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method.equals("GET")){
            // request method is GET
            Log.e(TAG, "check1");
            DefaultHttpClient httpClient = new DefaultHttpClient();
            Log.e(TAG, "check2");
            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false);
            Log.e(TAG, "check3");
            String paramString = URLEncodedUtils.format(params, "utf-8");
            Log.e(TAG, "check4");
            url += "?" + paramString;
            Log.e(TAG, "check5");
            HttpGet httpGet = new HttpGet(url);
            Log.e(TAG, "check6");

            HttpResponse httpResponse = httpClient.execute(httpGet); //getting exception here
            Log.e(TAG, "check7");
            HttpEntity httpEntity = httpResponse.getEntity();
            Log.e(TAG, "check8");
            is = httpEntity.getContent();
            Log.e(TAG, "check9");
        }           

    }
    catch (UnsupportedEncodingException e) {

        Log.e(TAG, "e1"+e.toString());
        return null;
    }
    catch (ClientProtocolException e) {
        Log.e(TAG, "e2"+e.toString());
        return null;
    }
    catch (IOException e) {
        Log.e(TAG, "e3"+e.toString());//this exception occurs
        return null;
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e(TAG, "e4"+" Error converting result " + e.toString());
        return null;
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e(TAG, "e5"+" Error parsing data " + e.toString());
        return null;
    }

    // return JSON String
    return jObj;

  }
}

When I am calling the above code with an object jParser of above class, it gives exception.

private final String GET_ALL_MODEL_NAME = "http://10.0.0.2/android_connect/get_all_model_name.php";

List<NameValuePair> params = new ArrayList<NameValuePair>();

// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(GET_ALL_MODEL_NAME, "GET", params);

I am getting the exception. my logcat is

02-23 03:52:55.702: E/ACCESS(1603): check1
02-23 03:52:55.732: E/ACCESS(1603): check2
02-23 03:52:55.732: E/ACCESS(1603): check3
02-23 03:52:55.742: E/ACCESS(1603): check4
02-23 03:52:55.742: E/ACCESS(1603): check5
02-23 03:52:55.792: E/ACCESS(1603): check6
02-23 03:56:22.442: E/ACCESS(1603): e3org.apache.http.NoHttpResponseException: The target server failed to respond

Please help, as I googled but find nothing helpful

Hemant Patel
  • 3,160
  • 1
  • 20
  • 29
  • Please check this post it helps you. http://stackoverflow.com/questions/24970431/android-the-target-server-failed-to-respond – Rishi Jul 29 '14 at 10:10

1 Answers1

2

It's obvious it's not a router-firewall related problem as you are under the same net, so there are only a few possibilities:

  • There's nothing listening on that port on that IP on the server-side
  • There's a local firewall on the server-side that is blocking that connection attempt
  • You are not using WIFI so you're not under the same net.

You should make sure you can open that service some ther way, that would help you debugging where the culprit is. If you've already done this, I'd suggest using some debugging tool to trace TCP packets (I don't know either what kind of operating system you use on the destination machine; if it's some linux distribution, tcpdump might help).

All that assuming you have the android.permission.INTERNET permission in your AndroidManifest.xml file.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • I have window 7 on which wamp is installed. Any suggestion regarding settings in wamp. As when i am running the url http://127.0.0.1/android_connect/get_all_model_name.php from my system browser its working and giving desired output. – Hemant Patel Feb 23 '14 at 10:14
  • I am running my app on emulator – Hemant Patel Feb 23 '14 at 10:14
  • You're probably missing the destination IP address, or your WAMP server could not be listening for not-localhost addresses. – nKn Feb 23 '14 at 10:19
  • 1
    "You are not using WIFI so you're not under the same net...." I plugged out the cable and problem solved, thank you nKn. – Mustafa Güven Jul 07 '14 at 13:20