Im making an Android application, and for that i have to get JSON content from a URL (which is output by some python script).
I found a method for that on SO, but i can’t get it to work. This is the code:
public String webGet(String URL) throws Exception
{
BufferedReader in = null;
try
{
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type", "text/html; charset=utf-8");
request.setURI(new URI(URL));
// Crashes after executing this line
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
{
sb.append(line + NL);
}
in.close();
String page = sb.toString();
//System.out.println(page);
return page;
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
Log.d("BBB", e.toString());
}
}
}
}
This crash happends after this line:
HttpResponse response = client.execute(request);
It then simply jumps to the finally part and all i can get from the exception message is that something was null. That’s all it says.
Anyone any idea what the problem could be?
Screenshot of the Exception description:

You cannot perform network actions from your ‘main’ thread within certain versions of Android. It’s to ensure your app is still responsive to the end user, even if it does some blocking, network action that takes a long time.
You would need to do that action another thread.
Unable to connect to FTP in Android