0

I have an app that access some JSON data from my website. The app works fine on WIFI and mobile data on my phone and the phones that I tested. However, some users are having problem on the mobile data. When they use WIFI, it works fine, but it doesn't work on their mobile data.

Here's the code for getting the json data.

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from url
        JSONObject json = jParser.makeHttpRequest(url, "GET", params);

It seems the json returns null for the mobile data on their phones. I'm not sure why.

Here's the class for the JSON Parsers

 public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// 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 == "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 == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
           // String paramString = URLEncodedUtils.format(params, "utf-8");
           // url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            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 parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}
 }
Julia
  • 1,207
  • 4
  • 29
  • 47
  • I think when you use mobile data, your app need some time request to server. You can init interface wait response from server. – Dungnbhut Sep 14 '17 at 02:17
  • use this answer for api request https://stackoverflow.com/questions/46086118/getting-json-info-from-php-to-android-studio?answertab=votes#tab-top – Vinayak B Sep 14 '17 at 03:49

1 Answers1

0

Try to set the connection and socket timeouts to reasonable amount of time -- test it on your device in area where your data connection is a bit slower in order to make a determination for how long that should be.

Here is a great SO answer with an example of how to do this: https://stackoverflow.com/a/1565243/5095571

app-dev
  • 348
  • 1
  • 2
  • 12