我有一个从我的服务器(在线)获取信息的应用程序,我的计算机中有该网站的版本(localhost),
应用程序正在使用localhost可以获取json,但我无法访问服务器上的网站。
新 HttpAsyncTask()执行( “http://alarbaeen.com/app/?a=gallery&b=images&c=new&d=1”);
HttpAsyncTask类:
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
try {
JSONObject json = new JSONObject(result);
etResponse.setText(json.toString(1));
} catch (Exception e) {
// TODO: handle exception
}
}
}
public static String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e)
{
}
return result;
}
这些是错误:
03-05 12:24:32.735: E/AndroidRuntime(5859): FATAL EXCEPTION: AsyncTask #1
03-05 12:24:32.735: E/AndroidRuntime(5859): Process: com.example.testjsons22, PID: 5859
03-05 12:24:32.735: E/AndroidRuntime(5859): java.lang.RuntimeException: An error occured while executing doInBackground()
03-05 12:24:32.735: E/AndroidRuntime(5859): at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-05 12:24:32.735: E/AndroidRuntime(5859): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.lang.Thread.run(Thread.java:841)
03-05 12:24:32.735: E/AndroidRuntime(5859): Caused by: java.lang.NullPointerException: println needs a message
03-05 12:24:32.735: E/AndroidRuntime(5859): at android.util.Log.println_native(Native Method)
03-05 12:24:32.735: E/AndroidRuntime(5859): at android.util.Log.i(Log.java:160)
03-05 12:24:32.735: E/AndroidRuntime(5859): at com.example.testjsons22.MainActivity.GET(MainActivity.java:77)
03-05 12:24:32.735: E/AndroidRuntime(5859): at com.example.testjsons22.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:108)
03-05 12:24:32.735: E/AndroidRuntime(5859): at com.example.testjsons22.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:1)
03-05 12:24:32.735: E/AndroidRuntime(5859): at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-05 12:24:32.735: E/AndroidRuntime(5859): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-05 12:24:32.735: E/AndroidRuntime(5859): ... 4 more
答案 0 :(得分:0)
JSON是URL编码的。试试这个:
try{
JSONObject json = new JSONObject(URLDecoder.decode(result, "UTF-8"));
etResponse.setText(json.toString(1));
} catch (Exception e) {
// TODO: handle exception
}
我不知道你在哪里调用这个AsyncTask,但是我看到的唯一可能返回运行时的其他错误是静态方法没有被初始化。尝试在AsyncTask中移动代码。