-3

Just trying to grab a small bit of information from a php file on our MySql server to show a connection was made by the app. At this point it now crashes the app and spit out org.Apache.http.client.clientprotocolexecption, forgive me if this is a noob question, I'm only 4 months into programing in java.

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)

Code:

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();
        }

    });
Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
Glenn
  • 79
  • 1
  • 10

2 Answers2

3

you should use AsyncTask for the process

      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);
               }

}

and then simply

test.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
         new A().execute();
    }
});
user2717954
  • 1,822
  • 2
  • 17
  • 28
0

Your problem is that you're trying to set text on view outside UI thread. So you've created your new thread for http access, and now you call setText from the same thread. As suggested above, use AsyncTask.