Android HttpGet请求

时间:2013-10-07 08:03:24

标签: android

我试图从服务器获取一些json字符串,但是当我想连接到服务器时收到此错误:

  

10-07 10:57:47.410:E / AndroidRuntime(11050):致命异常:主要   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):at java.lang.reflect.Method.invokeNative(Native Method)   10-07 10:57:47.410:E / AndroidRuntime(11050):at java.lang.reflect.Method.invoke(Method.java:511)   10-07 10:57:47.410:E / AndroidRuntime(11050):at 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):引起: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):at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)   10-07 10:57:47.410:E / AndroidRuntime(11050):at libcore.io.IoBridge.connectErrno(IoBridge.java:127)   10-07 10:57:47.410:E / AndroidRuntime(11050):at libcore.io.IoBridge.connect(IoBridge.java:112)   10-07 10:57:47.410:E / AndroidRuntime(11050):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)   10-07 10:57:47.410:E / AndroidRuntime(11050):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)   10-07 10:57:47.410:E / AndroidRuntime(11050):at java.net.Socket.connect(Socket.java:848)   10-07 10:57:47.410:E / AndroidRuntime(11050):at 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):at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)   10-07 10:57:47.410:E / AndroidRuntime(11050):at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)   10-07 10:57:47.410:E / AndroidRuntime(11050):at 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):at 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):at com.example.stampp.Services.ConfigurateAPI.ConnectApi.postAuthorization(ConnectApi.java:51)   10-07 10:57:47.410:E / AndroidRuntime(11050):at 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();
    }
}

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:2)

由于 Android 3.0 ,您无法在主线程上使用网络连接。您最好使用AsynTask创建doInBackground来执行httpget

答案 1 :(得分:0)

使用一些AsyncTask来完成它。请参阅this基本示例。