Java Android:JSON不是';t工作,Log.e返回空值

Java Android:JSON不是';t工作,Log.e返回空值,java,android,json,Java,Android,Json,我试图从这里获得比特币的美元平均价格30d:。我的Json代码在try/catch中不起作用,错误为null(我尝试研究其他问题,但它们似乎都返回错误,而我的代码只返回null): 任何帮助都将不胜感激!谢谢。试试这段代码 private void get_value(String php) { String responseString = null; try{ HttpClient httpclient = new DefaultHttpClient(); String

我试图从这里获得比特币的美元平均价格30d:。我的Json代码在try/catch中不起作用,错误为null(我尝试研究其他问题,但它们似乎都返回错误,而我的代码只返回null):

任何帮助都将不胜感激!谢谢。

试试这段代码

private void get_value(String php) {
 String responseString = null;
 try{    
  HttpClient httpclient = new DefaultHttpClient();
  String url ="your_url"; 
  HttpPost httppost = new HttpPost(url);
  HttpResponse response = httpclient.execute(httppost);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  response.getEntity().writeTo(out);
  out.close();
  responseString = out.toString();
  Toast.makeText(getApplicationContext(),responseString,1000).show();

  //The below code is for Separating values.It may varies according with your result

  JSONArray ja = new JSONArray(responseString);
  int x=Integer.parseInt(ja.getJSONObject(0).getString("your_string_name"));

} catch(Exception e) {
}
}

“空”是什么意思?这是否意味着NullPointerException?如果是这样,您需要发布堆栈跟踪。使用
AsyncTask
进行web调用可能是错误的,但我似乎记得如果您调用
e.printStackTrace()
它将清除消息。这意味着,当您尝试在代码中使用
e.getMessage()
时,它只会返回“null”作为消息字符串。此时使用
Log.e(…)
没有任何意义,因为
e.printStackTrace()
将转储整个异常堆栈跟踪,包括消息和原因。我无法在5分钟后编辑我的评论,但正如@SathishKumar所说,您应该使用
AsyncTask
或类似工具来执行网络操作。你得到的例外可能是告诉你不应该在主(UI)线程上执行网络操作。多亏@SathishKumar建议执行异步任务,我才让它正常工作。谢谢大家!顺便说一句,当我试图找到错误时,它给出了一个空值。
private void get_value(String php) {
 String responseString = null;
 try{    
  HttpClient httpclient = new DefaultHttpClient();
  String url ="your_url"; 
  HttpPost httppost = new HttpPost(url);
  HttpResponse response = httpclient.execute(httppost);
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  response.getEntity().writeTo(out);
  out.close();
  responseString = out.toString();
  Toast.makeText(getApplicationContext(),responseString,1000).show();

  //The below code is for Separating values.It may varies according with your result

  JSONArray ja = new JSONArray(responseString);
  int x=Integer.parseInt(ja.getJSONObject(0).getString("your_string_name"));

} catch(Exception e) {
}
}