Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仍然得到android.os.NetworkOnMainThreadException,我做错了什么?_Java_Android - Fatal编程技术网

Java 仍然得到android.os.NetworkOnMainThreadException,我做错了什么?

Java 仍然得到android.os.NetworkOnMainThreadException,我做错了什么?,java,android,Java,Android,只是想从MySql服务器上的php文件中获取一点信息,以显示应用程序建立了连接。此时,它会使应用程序崩溃,并抛出org.Apache.http.client.clientprotocolexecption,如果这是一个noob问题,请原谅,我用java编程才4个月 logcat: 11-06 09:51:20.268: E/log.tag(2748): Error in http connection org.apache.http.client.ClientProtocolException

只是想从MySql服务器上的php文件中获取一点信息,以显示应用程序建立了连接。此时,它会使应用程序崩溃,并抛出org.Apache.http.client.clientprotocolexecption,如果这是一个noob问题,请原谅,我用java编程才4个月

logcat:

11-06 09:51:20.268: E/log.tag(2748): Error in http connection  org.apache.http.client.ClientProtocolException
    11-06 09:51:20.268: W/dalvikvm(2748): threadid=13: thread exiting with uncaught exception (group=0x40a13300)
    11-06 09:51:20.288: E/AndroidRuntime(2748): FATAL EXCEPTION: Thread-83
    11-06 09:51:20.288: E/AndroidRuntime(2748): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.View.invalidate(View.java:10250)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.View.invalidate(View.java:10205)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.checkForRelayout(TextView.java:6296)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3547)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3405)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3380)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at com.mobile.donswholesale.AppInfo$1$1.run(AppInfo.java:111)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at java.lang.Thread.run(Thread.java:856)
test.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new Thread(new Runnable() {

                public void run() {

                    try {

                        HttpParams httpParams = new BasicHttpParams();
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httpost = new HttpPost("http://"
                                + serverIp.getText().toString()
                                + "/mobile.php");
                        httpost.setHeader("Accept", "application/json");
                        HttpResponse response = httpclient.execute(httpost);
                        HttpEntity entity = response.getEntity();
                        isr = entity.getContent();
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error in http connection  " + e.toString());
                        resultView.setText("Could not connect to Database");
                    }
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(isr, "iso=8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        isr.close();

                        result = sb.toString();
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error converting result  " + e.toString());
                    }

                    try {
                        String s = "";
                        JSONArray jArray = new JSONArray(result);
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json = jArray.getJSONObject(i);
                            s = s + "User :" + json.getString("UserName");
                        }
                        resultView.setText(s);
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error Parsing Data " + e.toString());
                    }

                    Log.d(MainActivity.DEBUGTAG, "Didn't Work ");
                    return;
                }

                protected void onPostExecute(Void results) {

                }

            }).start();
        }

    });
代码:

11-06 09:51:20.268: E/log.tag(2748): Error in http connection  org.apache.http.client.ClientProtocolException
    11-06 09:51:20.268: W/dalvikvm(2748): threadid=13: thread exiting with uncaught exception (group=0x40a13300)
    11-06 09:51:20.288: E/AndroidRuntime(2748): FATAL EXCEPTION: Thread-83
    11-06 09:51:20.288: E/AndroidRuntime(2748): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.View.invalidate(View.java:10250)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.view.View.invalidate(View.java:10205)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.checkForRelayout(TextView.java:6296)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3547)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3405)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at android.widget.TextView.setText(TextView.java:3380)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at com.mobile.donswholesale.AppInfo$1$1.run(AppInfo.java:111)
    11-06 09:51:20.288: E/AndroidRuntime(2748):     at java.lang.Thread.run(Thread.java:856)
test.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new Thread(new Runnable() {

                public void run() {

                    try {

                        HttpParams httpParams = new BasicHttpParams();
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httpost = new HttpPost("http://"
                                + serverIp.getText().toString()
                                + "/mobile.php");
                        httpost.setHeader("Accept", "application/json");
                        HttpResponse response = httpclient.execute(httpost);
                        HttpEntity entity = response.getEntity();
                        isr = entity.getContent();
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error in http connection  " + e.toString());
                        resultView.setText("Could not connect to Database");
                    }
                    try {
                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(isr, "iso=8859-1"), 8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                        isr.close();

                        result = sb.toString();
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error converting result  " + e.toString());
                    }

                    try {
                        String s = "";
                        JSONArray jArray = new JSONArray(result);
                        for (int i = 0; i < jArray.length(); i++) {
                            JSONObject json = jArray.getJSONObject(i);
                            s = s + "User :" + json.getString("UserName");
                        }
                        resultView.setText(s);
                    } catch (Exception e) {
                        Log.e("log.tag",
                                "Error Parsing Data " + e.toString());
                    }

                    Log.d(MainActivity.DEBUGTAG, "Didn't Work ");
                    return;
                }

                protected void onPostExecute(Void results) {

                }

            }).start();
        }

    });
test.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新线程(newrunnable()){
公开募捐{
试一试{
HttpParams HttpParams=新的BasicHttpParams();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://”
+serverIp.getText().toString()
+“/mobile.php”);
setHeader(“接受”、“应用程序/json”);
HttpResponse response=httpclient.execute(httpost);
HttpEntity=response.getEntity();
isr=entity.getContent();
}捕获(例外e){
Log.e(“Log.tag”,
“http连接错误”+e.toString());
resultView.setText(“无法连接到数据库”);
}
试一试{
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(isr,“iso=8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
isr.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log.tag”,
“转换结果时出错”+e.toString());
}
试一试{
字符串s=“”;
JSONArray jArray=新JSONArray(结果);
for(int i=0;i
您应该为流程使用AsyncTask

      class A extends AsyncTask<Void, Void, String> {
          protected String doInBackground(Void... params) {
               String result = "";
               try {

                   HttpParams httpParams = new BasicHttpParams();
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httpost = new HttpPost("http://"
                            + serverIp.getText().toString()
                            + "/mobile.php");
                    httpost.setHeader("Accept", "application/json");
                    HttpResponse response = httpclient.execute(httpost);
                    HttpEntity entity = response.getEntity();
                    isr = entity.getContent();
                } catch (Exception e) {
                    Log.e("log.tag",
                            "Error in http connection  " + e.toString());
                    return null;
                    //resultView.setText("Could not connect to Database");
                }
                try {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(isr, "iso=8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    isr.close();

                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log.tag",
                            "Error converting result  " + e.toString());
                }

                try {
                    String s = "";
                    JSONArray jArray = new JSONArray(result);
                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json = jArray.getJSONObject(i);
                        s = s + "User :" + json.getString("UserName");
                    }
                    return s;
                } catch (Exception e) {
                    Log.e("log.tag",
                            "Error Parsing Data " + e.toString());
                }
          }
          protected void onPostExecute(String s) {
               if (s == null) {
                    resultView.setText("Could not connect to Database");
               }
               else {
                   resultView.setText(s);
               }

}

您的问题是您试图在UI线程之外的视图上设置文本。因此,您已经为http访问创建了新线程,现在您可以从同一线程调用setText。如上所述,使用AsyncTask

您是否添加了权限:什么是logcat输出?我怀疑您是否得到了您认为的异常,因为线程看起来很好,但是您似乎正在尝试从非UI线程更新某些UI元素,而您无法做到是internet已启用,log cat:HTTP连接org.Apache.HTTP.client.ClientProtocolExecOption中的错误。“只有创建视图层次结构的原始线程才能接触其视图。”它就在stacktrace中,您可能没有读过。