Pergunta

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

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top