0

I got data from reader.readLine(). But it doesn't enter the while loop. The return 'json' string is null.

Someone please help me to solve the issue. i have already tried a lot for a correct solution.

try {            
    URL urL = new URL(url);
    urlConnection = (HttpURLConnection) urL.openConnection();
    urlConnection.setRequestMethod("POST");
    urlConnection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    urlConnection.addRequestProperty("Accept", "application/json");
    urlConnection.setConnectTimeout(10000);
    urlConnection.setReadTimeout(15000);
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    Uri.Builder builder = new Uri.Builder()
            .appendQueryParameter("PackageName", packagename);

    String query = builder.build().getEncodedQuery();

    OutputStream os = urlConnection.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
    writer.write(query);
    writer.flush();
    writer.close();
    os.close();

    urlConnection.connect();

    InputStream in = new BufferedInputStream(urlConnection.getInputStream());

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuffer result = new StringBuffer();

    Log.e("READER","....."+reader.readLine());
    String line=reader.readLine();
    while (line != null) {
        result.append(line).append('\n');
    }
    json=result.toString();
    in.close();

} catch (Exception e) {
    e.printStackTrace();
} finally {
    urlConnection.disconnect();
}
Log.e("RESULT","...."+json);
return json;
Jaap
  • 81,064
  • 34
  • 182
  • 193
Aparna.M
  • 11
  • 6
  • I think your code includes `try {` ___ look just above the code snippet you shared ___ – Rafaf Tahsin Sep 27 '16 at 08:59
  • 1
    No the try block is closed before catch statement – Aparna.M Sep 27 '16 at 09:27
  • 1
    Atlast I got it. The issue was on the line Log.e("READER","....."+reader.readLine()); String line=reader.readLine(); I changed the above lines as String line=reader.readLine(); Log.e("READER","....."+line); Now its working fine... Thank you all for the support..:) – Aparna.M Sep 27 '16 at 10:28

3 Answers3

1

Remove the extra reader.readLine() in the log,

Log.e("READER","....." + reader.readLine());

Try this code,

Log.e("ResponseCode", "" + urlConnection.getResponseCode());

InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer result = new StringBuffer();

String line;
while ((line = reader.readLine()) != null) {
    result.append(line);
}
json = result.toString();
in.close();
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
0

Do this it's basic maybe you missed it :

   json=result.toString();
    in.close();

} catch (Exception e) {
    e.printStackTrace();
} finally {
    urlConnection.disconnect();
}
Log.e("RESULT","...."+json);
if (json!=null)
return json;

It will skip null pointer exception:

DevKRos
  • 422
  • 2
  • 15
0

If all you are doing is downloading a jsonString from the API, this should sort you out:

String jsonString = "";

    try {

        URL mURL = new URL(yourURL);

        HttpURLConnection mHttpURLConnection;
        mHttpURLConnection = (HttpURLConnection)mURL.openConnection();

        mHttpURLConnection.setDoInput(true);

        mHttpURLConnection.connect();

        InputStream mInputStream = mHttpURLConnection.getInputStream();

        int i = 0;
        while ((i = mInputStream.read()) != -1) {
            jsonString += (char) i;
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();

    } catch (IOException e) {
        e.printStackTrace();

    }
Log.e("JSON","Json String received: " + jsonString);
badrobot15
  • 176
  • 1
  • 10