我的Http Post不起作用

时间:2013-10-28 13:55:53

标签: java json http-post android-studio

我有一些获取信息的方法。
我想通过 http post 将这些作为 json对象发送到 php服务器。 下面的代码不起作用......

 //  Methods to get the information for the JsonObject.

            JSONObject obj= new JSONObject();
        try {
            obj.put("Model", ReadIn1);
            obj.put("Product",ReadIn2);
            obj.put("Manufacturer",ReadIn3);
            obj.put("RELEASE",ReadIn4);
            obj.put("SERIAL",ReadIn5);
            obj.put("ID",ReadIn6);
            obj.put("ANDROID_ID",ReadIn7);
            obj.put("Language",ReadIn8);
            obj.put("BOARD",ReadIn9);
            obj.put("BOOTLOADER",ReadIn10);
            obj.put("BRAND",ReadIn11);
            obj.put("CPU_API",ReadIn12);
            obj.put("DISPLAY",ReadIn13);
            obj.put("FINGERPRINT",ReadIn14);
            obj.put("HARDWARE",ReadIn15);
            obj.put("UUID",ReadIn16);
            // obj.put("Vname",ReadIn17);
            // obj.put("Vcode",ReadIn18);
            //  StringEntity se = new StringEntity(obj.toString());


            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 1000);
            HttpConnectionParams.setSoTimeout(httpParams, 1000);
            HttpClient client = new DefaultHttpClient(httpParams);

            // Unbind failed: could not find connection for android.os.BinderProxy@413a65f8


            String url = "http://localhost/index.html";
            HttpPost post = new HttpPost(url);
            post.setEntity(new ByteArrayEntity(obj.toString().getBytes(
                    "UTF8")));
            post.setHeader("json", obj.toString());
            HttpResponse response = client.execute(post); // from here on the code jumps into the catch blog
            HttpEntity entity = response.getEntity();



            if (entity != null) {
                InputStream instream = entity.getContent();

                String result = RestClientJ.convertStreamToString(instream);
                Log.i("Read from server", result);

            }
        } catch (Throwable t) {
            Log.i("CATCH Read from server", "error");

          }
        }
    }
);}

Rest Client Class

public class RestClientJ {

    public static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new
                InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}

有人可以解释我为什么我的http Post不起作用吗? 我得到android.os.NetworkOnMainThreadException as error message... 我该如何解决这个问题?

logcat的

  

块引用   10-28 14:17:40.523 338-348 /? W / AlarmManager:FACTORY_ON = 0   10-28 14:17:44.695 338-389 /? V / AlarmManager:waitForAlarm结果:4   10-28 14:17:44.703 338-389 /? V / AlarmManager:触发ELAPSED_REALTIME_WAKEUP或RTC_WAKEUP   10-28 14:17:44.718 338-420 /? D / WifiP2pService:P2pDisabledState {what = 131204}   10-28 14:17:44.726 338-4597 /? W / AlarmManager:FACTORY_ON = 0   10-28 14:17:44.914 338-623 /? W / AlarmManager:FACTORY_ON = 0   10-28 14:17:44.921 338-420 /? D / WifiP2pService:P2pDisabledState {what = 131204}   10-28 14:17:48.781 338-2719 /? W / AlarmManager:FACTORY_ON = 0   10-28 14:17:59.992 338-389 /? V / AlarmManager:waitForAlarm结果:8   10-28 14:18:00.000 338-369 /? V / AlarmManager:ClockReceiver onReceive()ACTION_TIME_TICK   10-28 14:18:00.000 338-369 /? W / AlarmManager:FACTORY_ON = 0   10-28 14:18:00.007 444-444 /? D / STATUSBAR-Clock:onReceive() - ACTION_TIME_TICK   10-28 14:18:00.023 444-444 /? D / STATUSBAR-Clock:onReceive() - ACTION_TIME_TICK   10-28 14:18:32.367 444-444 /? D / STATUSBAR-NetworkController:onSignalStrengthsChanged signalStrength = SignalStrength:15 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 gsm | lte 4 level = 4   10-28 14:18:32.375 338-1841 /? W / AlarmManager:FACTORY_ON = 0

0 个答案:

没有答案