我想使用JSON并从网络上获取数据。我找到了一个教程,我应用了任何东西。结果是沮丧。简而言之:
致命异常:AsyncTask#1
java.lang.RuntimeException:执行时发生错误 doInBackground()
这是我的代码:
package com.example.winsekiz.searchlist;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class WebServer extends Activity {
final static String URI = "www.hocaogluticaret.com/a.html";
TextView tvData;
JSONObject json;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.turkey);
tvData = (TextView) findViewById(R.id.tv);
new Game().execute("text");
}
protected JSONObject readGameParks() throws ClientProtocolException, IOException, JSONException{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URI);
HttpResponse response =client.execute(get);
StatusLine status = response.getStatusLine();
int s = status.getStatusCode();
if(s == 200) {
HttpEntity e = response.getEntity();
String data = EntityUtils.toString(e);
JSONArray posts = new JSONArray(data);
JSONObject last = posts.getJSONObject(0);
}
return null;
}
public class Game extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
try{
json = readGameParks();
String data = json.getString(params[0]);
return data;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data){
tvData.setText(data);
}
}
}
这是错误:
03-21 22:29:17.110 10922-10922/com.example.winsekiz.searchlist I/art: Not late-enabling -Xcheck:jni (already on)
03-21 22:29:17.120 10922-10922/com.example.winsekiz.searchlist W/System: ClassLoader referenced unknown path: /data/app/com.example.winsekiz.searchlist-2/lib/x86_64
03-21 22:29:17.130 10922-10935/com.example.winsekiz.searchlist D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-21 22:29:17.200 10922-10935/com.example.winsekiz.searchlist I/OpenGLRenderer: Initialized EGL, version 1.4
03-21 22:29:17.270 10922-10935/com.example.winsekiz.searchlist W/EGL_emulation: eglSurfaceAttrib not implemented
03-21 22:29:17.270 10922-10935/com.example.winsekiz.searchlist W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7feb1eea0e40, error=EGL_SUCCESS
03-21 22:29:18.910 10922-10944/com.example.winsekiz.searchlist E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.winsekiz.searchlist, PID: 10922
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.hocaogluticaret.com/a.html
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:603)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:299)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at com.example.winsekiz.searchlist.WebServer.readGameParks(WebServer.java:47)
at com.example.winsekiz.searchlist.WebServer$Game.doInBackground(WebServer.java:68)
at com.example.winsekiz.searchlist.WebServer$Game.doInBackground(WebServer.java:62)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
03-21 22:29:18.990 10922-10935/com.example.winsekiz.searchlist W/EGL_emulation: eglSurfaceAttrib not implemented
03-21 22:29:18.990 10922-10935/com.example.winsekiz.searchlist W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7feb1d9ff2c0, error=EGL_SUCCESS
03-21 22:29:19.010 10922-10935/com.example.winsekiz.searchlist E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2daa90
03-21 22:29:24.090 10922-10935/com.example.winsekiz.searchlist E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7feb2e2daf60
03-21 22:34:18.920 10922-10944/? I/Process: Sending signal. PID: 10922 SIG: 9
答案 0 :(得分:1)
您的网址不完整。
www.hocaogluticaret.com/a.html
应该是
http://www.hocaogluticaret.com/a.html
或
https://www.hocaogluticaret.com/a.html
答案 1 :(得分:1)
让步骤通过代码
1)启动AsyncTask
new Game().execute("text");
2)去doInBackground
json = readGameParks();
3)连接到不合格的URI
HttpGet get = new HttpGet("www.hocaogluticaret.com/a.html");
并且您收到错误,要解决此问题,请将http://
放在地址前面。
4)假设已正确建立连接,请继续return null;
String data = json.getString(params[0]);
return data;
此处params[0]
为&#34; text&#34;,但json
为空,因此抛出NullPointerException
。
要解决此问题,请尝试不在null
中返回readGameParks
。我认为你应该在状态码检查中做return last;
。
在代码中,
JSONObject last = new JSONObject();
if(s == 200) {
HttpEntity e = response.getEntity();
String data = EntityUtils.toString(e);
JSONArray posts = new JSONArray(data);
last = posts.getJSONObject(0);
}
return last;