我正在做一个小型的android项目,第一个功能是连接到我的服务器并执行一个php解析器来获取一些文章。我的on create调用了这个方法:
private String jsonTaker() throws IOException, JSONException {
//call EventsTaker class -> take the 'articles'
JsonReader jsr = new JsonReader();
JSONObject json = jsr.readJsonFromUrl("http://jem88.net/timeSpaceParser.php");
System.out.println(json.toString());
System.out.println(json.get("id"));
return json.toString();
}
这是readJsonFromUrl()
和readAll()
方法:
public JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL (url).openStream();
//sia maledetto il creatore di java e tutta la sua genia diojavosogeloso
InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
BufferedReader rd = new BufferedReader(isr);
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
Log.d("readJsonFromUrl", "String to json transformed!");
is.close();
return json;
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
在调试模式下出现错误,执行到达readJsonFromUrl()
方法的第一行,我尝试打开流。这里的应用程序因“找不到源”错误而崩溃。我试过谷歌搜索这个,但没有任何一个给出的建议对我有好处!所以,任何建议都会很棒!
编辑 - >我编辑了异常的管理,现在我抓住了一个致命的例外。这是logcat:
06-04 23:46:56.411: D/AndroidRuntime(22456): Shutting down VM
06-04 23:46:56.411: W/dalvikvm(22456): threadid=1: thread exiting with uncaught exception (group=0x40e312a0)
06-04 23:46:56.436: E/AndroidRuntime(22456): FATAL EXCEPTION: main
06-04 23:46:56.436: E/AndroidRuntime(22456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jem88.timespace/com.jem88.timespace.MainActivity}: android.os.NetworkOnMainThreadException
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.Handler.dispatchMessage(Handler.java:99)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.Looper.loop(Looper.java:137)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.lang.reflect.Method.invoke(Method.java:511)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-04 23:46:56.436: E/AndroidRuntime(22456): at dalvik.system.NativeStart.main(Native Method)
06-04 23:46:56.436: E/AndroidRuntime(22456): Caused by: android.os.NetworkOnMainThreadException
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
06-04 23:46:56.436: E/AndroidRuntime(22456): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
06-04 23:46:56.436: E/AndroidRuntime(22456): at java.net.URL.openStream(URL.java:462)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.JsonReader.readJsonFromUrl(JsonReader.java:44)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.MainActivity.jsonTaker(MainActivity.java:46)
06-04 23:46:56.436: E/AndroidRuntime(22456): at com.jem88.timespace.MainActivity.onCreate(MainActivity.java:23)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.Activity.performCreate(Activity.java:5206)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-04 23:46:56.436: E/AndroidRuntime(22456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-04 23:46:56.436: E/AndroidRuntime(22456): ... 11 more
答案 0 :(得分:0)
只是把它放在分开的线程而不是主线程中,我自己测试了你的代码
Thread tr=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Log.v("Alefon", jsonTaker());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("Alefon", "errr"+e.getMessage());
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("Alefon", "errr "+e.getMessage());
}
}
});
tr.start();
使用AsyncTask的做法会更好,请参阅:link