我试图从服务器获取一些 json 字符串,但是当我想连接到服务器时收到此错误:
10-07 10:57:47.410: E/AndroidRuntime(11050): 致命异常: main 10-07 10:57:47.410: E/AndroidRuntime(11050): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.stampp/com.example.stampp.UI.MainActivity}:android.os.NetworkOnMainThreadException 10-07 10:57:47.410:E/AndroidRuntime(11050):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970 ) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android .app.ActivityThread.access$600(ActivityThread.java:128) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161) 10-07 10:57:47.410:E/AndroidRuntime(11050):在 android.os.Handler。dispatchMessage(Handler.java:99) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.os.Looper.loop(Looper.java:137) 10-07 10:57:47.410: E/ AndroidRuntime(11050): 在 android.app.ActivityThread.main(ActivityThread.java:4517) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 java.lang.reflect.Method.invoke(Method.java:511) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 10-07 10:57:47.410: E/AndroidRuntime(11050): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:760) 10-07 10:57:47.410: E/AndroidRuntime(11050): at dalvik.system.NativeStart.main(Native Method) 10-07 10:57:47.410: E/AndroidRuntime(11050): Caused作者:android.os。NetworkOnMainThreadException 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 10-07 10:57:47.410: E/AndroidRuntime(11050):在 libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 libcore.io.IoBridge.connectErrno(IoBridge.java:127) 10-07 10 :57:47.410: E/AndroidRuntime(11050): 在 libcore.io.IoBridge.connect(IoBridge.java:112) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 java.net.PlainSocketImpl。连接(PlainSocketImpl.java:192)10-07 10:57:47.410:E/AndroidRuntime(11050):在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)10-07 10:57:47.410:E/ AndroidRuntime(11050):在 java.net.Socket.connect(Socket.java:848) 10-07 10:57:47.410:E/AndroidRuntime(11050):在 org.apache.http。conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 10-07 10:57:47.410: E/AndroidRuntime(11050): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144 ) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-07 10:57:47.410: E/AndroidRuntime (11050):在 org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-07 10:57:47.410:E/AndroidRuntime(11050):在 org.apache.http.impl。 client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-07 10:57:47.410: E/AndroidRuntime(11050): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:674) 10 -07 10:57:47.410:E/AndroidRuntime(11050):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511) 10-07 10:57:47.410: E/AndroidRuntime(11050): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient .java:489) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 com.example.stampp.Services.ConfigurateAPI.ConnectApi.postAuthorization(ConnectApi.java:51) 10-07 10:57:47.410 : E/AndroidRuntime(11050): 在 com.example.stampp.UI.MainActivity.onCreate(MainActivity.java:61) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.app.Activity。 performCreate(Activity.java:4470) 10-07 10:57:47.410: E/AndroidRuntime(11050): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053) 10-07 10:57:47.410: E/ AndroidRuntime(11050): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934) 10-07 10:57:47.410: E/AndroidRuntime(11050): ... 11 更多
这是我的代码:
public void getAuthorization(String password, String user) {
String responseString="empty response from api";
String userPassword = user+":"+password;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(_url);
httpGet.setHeader("Authorization", "Basic "+Base64.encode(userPassword.getBytes(),Base64.NO_WRAP));
HttpResponse httpResponse = httpClient.execute(httpGet);
StatusLine statusLine = httpResponse.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
httpResponse.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
有人可以帮助我吗?