Note that there are some explanatory texts on larger screens.

plurals
  1. POentity is null.Why?
    text
    copied!<p>I want to read a JSON of a URL. I searched and test and now i think, i use asynTask because before i don't use asynTask, log writes android.os.NetworkOnMainThreadException. I write this program but i think entity is null becuase log.i in if(entity!=null) not write! I don't know why!</p> <p>GetJSONFromUrl.java</p> <pre><code> @SuppressLint("NewApi") class GetJSONFromUrl extends AsyncTask&lt;String, Void, Void&gt; { InputStream is = null; String result = ""; String error_text=""; JSONObject j = null; @Override protected Void doInBackground(String... urls) { try{ // Create a new HttpClient and Post Header DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(urls[0]); String resultString = null; Log.i("in function", "getJSONFrom"); long t = System.currentTimeMillis(); HttpResponse response = (HttpResponse) httpclient.execute(httpGet); System.out.println("HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]"); // Get hold of the response entity (-&gt; the data): HttpEntity entity = response.getEntity(); if (entity != null) { // Read the content stream InputStream instream = entity.getContent(); // convert content stream to a String resultString= convertStreamToString(instream); instream.close(); System.out.println("result String : " + resultString); Log.i("resultString: ", resultString); //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]" System.out.println("result String : " + resultString); // Transform the String into a JSONObject JSONObject jsonObjRecv = new JSONObject(resultString); // Raw DEBUG output of our received JSON object: System.out.println("&lt;JSONObject&gt;\n"+jsonObjRecv.toString()+"\n&lt;/JSONObject&gt;"); Log.i("jsonObjRecv: ", jsonObjRecv.toString()); } }catch(Exception e){e.printStackTrace();} return null; } protected void onPostExecute(String result) { 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(); result = sb.toString(); //System.out.println("Result = " + result); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } } private static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line =""; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } } </code></pre> <p>MainActivity.java</p> <pre><code>public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("testjson", "test"); new GetJSONFromUrl().execute("http://loyaltier.com/app/mobile/code/places/Maps.php"); } } </code></pre> <p>I see this in logCat:</p> <pre><code> 11-21 11:11:08.446: I/testjson(4756): test 11-21 11:11:08.446: I/in function(4756): getJSONFrom 11-21 11:11:08.454: W/System.err(4756): java.net.UnknownHostException: Unable to resolve host "loyaltier.com": No address associated with hostname 11-21 11:11:08.454: I/WindowManager(177): createSurface Window{42a3c470 org.example.testjson/org.example.testjson.MainActivity paused=false}: DRAW NOW PENDING 11-21 11:11:08.461: W/System.err(4756): at java.net.InetAddress.lookupHostByName(InetAddress.java:426) 11-21 11:11:08.461: W/System.err(4756): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 11-21 11:11:08.461: W/System.err(4756): at java.net.InetAddress.getAllByName(InetAddress.java:220) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 11-21 11:11:08.461: W/System.err(4756): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 11-21 11:11:08.461: W/System.err(4756): at org.example.testjson.GetJSONFromUrl.doInBackground(GetJSONFromUrl.java:48) 11-21 11:11:08.461: W/System.err(4756): at org.example.testjson.GetJSONFromUrl.doInBackground(GetJSONFromUrl.java:1) 11-21 11:11:08.461: W/System.err(4756): at android.os.AsyncTask$2.call(AsyncTask.java:264) 11-21 11:11:08.461: W/System.err(4756): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 11-21 11:11:08.461: W/System.err(4756): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 11-21 11:11:08.461: W/System.err(4756): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 11-21 11:11:08.461: W/System.err(4756): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 11-21 11:11:08.461: W/System.err(4756): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 11-21 11:11:08.461: W/System.err(4756): at java.lang.Thread.run(Thread.java:856) 11-21 11:11:08.461: W/System.err(4756): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname) 11-21 11:11:08.461: W/System.err(4756): at libcore.io.Posix.getaddrinfo(Native Method) 11-21 11:11:08.461: W/System.err(4756): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55) 11-21 11:11:08.461: W/System.err(4756): at java.net.InetAddress.lookupHostByName(InetAddress.java:411) 11-21 11:11:08.461: W/System.err(4756): ... 18 more </code></pre> <p>I run the program in my phone. Thanks . Cheers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload