2 qq 35159110 qq_35159110 于 2017.01.09 11:15 提问

ANDROID STUDIO网络问题

报错为:

01/09 11:01:50: Launching app
$ adb push C:\Users\Administrator\AndroidStudioProjects\MyApplication13\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.administrator.myapplication
$ adb shell pm install -r "/data/local/tmp/com.example.administrator.myapplication"
Success

$ adb shell am start -n "com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Connected to process 2346 on device emulator-5554
W/System: ClassLoader referenced unknown path: /data/app/com.example.administrator.myapplication-1/lib/x86
I/InstantRun: Instant Run Runtime started. Android package is com.example.administrator.myapplication, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.example.administrator.myapplication-1/lib/x86
W/art: Verification of void android.support.v7.app.AppCompatActivity.onSupportActionModeFinished(android.support.v7.view.ActionMode) took 1.372s
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
W/gralloc_ranchu: Gralloc pipe failed

              [ 01-09 03:02:02.837  2346: 2346 D/         ]
              HostConnection::get() New Host Connection established 0x9daf8380, tid 2346

I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
Application terminated.

在权限中,我已经添加了网络权限。
但是不知道为什么就是不行。
源代码如下:
package com.example.administrator.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static android.icu.lang.UCharacter.GraphemeClusterBreak.V;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView responseText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRquest=(Button)findViewById(R.id.send_request);
responseText=(TextView)findViewById(R.id.requestion_text);
sendRquest.setOnClickListener(this);

}
public void onClick(View v)
{
 if(v.getId()==R.id.send_request)
 {
   sendResquestWithHttpURLConnection();
 }

}
private void sendResquestWithHttpURLConnection()
{
 new Thread(new Runnable() {
     @Override
     public void run() {
         HttpURLConnection connection=null;
         BufferedReader reader=null;
         try{
         URL url=new URL("http://www.baidu.com");
             connection=(HttpURLConnection)url.openConnection();
             connection.setRequestMethod("GET");
             connection.setConnectTimeout(8000);
             connection.setReadTimeout(8000);
             InputStream in=connection.getInputStream();
             reader=new BufferedReader(new InputStreamReader(in));
             StringBuilder response=new StringBuilder();
            String line;
             while((line=reader.readLine())!=null)
             {
              response.append(in);

             }
             showResponse(response.toString());

         }
         catch(Exception e)
         {
           e.printStackTrace();
         }
         finally {
             if(reader!=null){
                 try{
                    reader.close();
                 }catch (IOException e)
                 {
                     e.printStackTrace();
                 }
             }
             if(connection!=null)
             {
                connection.disconnect();
             }

         }
     }
 }).start();

}
private  void showResponse(final String response){
    runOnUiThread(new Runnable(){
        public void run(){
            responseText.setText(response);
        }
    });
}

}

4个回答

dabocaiqq
dabocaiqq   2017.01.14 22:59
qq_33908388
qq_33908388   2017.05.24 11:49

你需要开启一个线程去执行,Android studio不允许在主线程中执行一些耗时的操作,你可以在onclick中
new Thread(new Runnable() {
@Override
public void run() {
//具体操作逻辑
}
}).start();

qq_33908388
qq_33908388   2017.05.24 11:50

你需要开启一个线程去执行网络连接服务,因为Android studio不允许在主线程中执行网络连接等耗时操作
new Thread(new Runnable() {
@Override
public void run() {
//具体操作逻辑
}
}).start();

qq_32808045
qq_32808045   2017.11.23 09:00
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!