1

I am developing an android application which accesses a MySQL database with PHP and JSON as parser. Currently I develop it just to retrieve the data from database table. It works well on emulator 2.2 (froyo), but when I try to run it on higher version of emulator, it doesn't retrieve the data. (I have searched a topic about this, but I didn't find any).

I guess there is something with the JSON class I'm using, or miss-using of HTTP connector in the JSON. Here I paste my JsonParse.java code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.http.*;
import android.util.Log;
public class JsonParse {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JsonParse() {

}

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
    try {
        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();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    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("Buffer Error", "Error Converting Result" + e.toString());
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error Parsing Data" + e.toString());
    }
    return jObj;

}
}

Is there something with that DefaultHTTPClientI'm using or should I use another one?

For the URL, I use a separated class called ServerProcessor.java which contains only one method like this:

public class ServerProcessor extends DbConnection {
String URL = "http://10.0.2.2/myURL/serverandroid.php";
String url = "";
String response = "";

public String retrieveData() {
    try {
        url = URL + "?operation=retrieve";
        response = call(url);
    } catch (Exception e) {
    }
    return response;
}
}

Please hand me steps to solve it. Thanks in advance

SvenT23
  • 667
  • 6
  • 19
FathulAzis
  • 49
  • 1
  • 2
  • 10

1 Answers1

0

Seems that you are not using a AsyncTask to make the request. Older android version would do that in the main thread, but newer version would throw an exception if you do this.

So you should look in the logcat whether an exception occurs

More info for networking in the developers guide: http://developer.android.com/training/basics/network-ops/connecting.html

maksim
  • 176
  • 7
  • Yes, I've read that. That's why I'm suspicious on that DefaultHttpClient, should I change it with HttpURLConnection? And where to place it? In a separated class or in the activity class. I am so sorry to ask like this because I am totally new in developing app like this. – FathulAzis May 09 '14 at 08:41
  • I would create another class extending a asynctask. Inside you would place the code where you make your DefaultHttpClient etc. – maksim May 09 '14 at 08:46
  • The defaultHttpClient isn't the problem. The fact that you're running your code on the main UI thread instead of a worker thread is most likely the problem. See [this](http://stackoverflow.com/questions/14250989/how-to-use-asynctask-correctly-android) as to how to use asynctask. – SvenT23 May 09 '14 at 08:47
  • @maksim Yes, I've just discussed it with my friend, and he said that these are my problems like you mention. Thank you for your advice. I'll try it – FathulAzis May 09 '14 at 09:13
  • @SvenT23 Thanks, the same reply as for maksim. Really appreciate it – FathulAzis May 09 '14 at 09:14
  • Cool, glad to help. I would appreciate if you flagged the answer as accepted – maksim May 09 '14 at 11:34