Android从(HTTP)POST到GET

时间:2013-01-04 11:18:00

标签: php android http-post http-get

我正在研究需要向php文件发送相同字符串的相同应用程序。当我在PHP中使用此代码进行检查时,iOs是一个GET。

file_put_contents('dump.txt', "POST: \n" . print_r($_POST, true) . "\n\n\n GET: \n" . print_r($_GET, true));

但Android的一个是POST,但它们需要完全相同,因为我已经构建了PHP的工作部分,我不能再改变了。

这是我的安卓代码:

HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpost = new HttpPost("http://Myserver.com/default.php");

            json = new JSONObject(); 
            try { 
                json.put("id", "69403"); 
                json.put("longitude", longi); 
                json.put("latitude", lat); 
                json.put("timestamp", time); 

            } catch (JSONException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            } 

            /// create StringEntity with current json obejct 

            try { 
          //  StringEntity se = new StringEntity(json.toString()); 

                List <NameValuePair> nvps = new ArrayList <NameValuePair>();
                nvps.add(new BasicNameValuePair("data", json.toString()));
                httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


                System.out.println("send about to do post");
                try {
                    response = httpclient.execute(httpost);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("send post done");
                HttpEntity entity = response.getEntity();


            } catch (UnsupportedEncodingException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            } 

2 个答案:

答案 0 :(得分:0)

变化:

HttpPost httpost = new HttpPost("http://Myserver.com/default.php");

HttpGet httpget = new HttpGet("http://Myserver.com/default.php");

答案 1 :(得分:0)

如果您收到NetworkOnMainThreadException,那是因为您正在尝试从主线程进行网络操作。如果是这样,请尝试查看AsyncTask

还要确保您在Android清单中获得了互联网权限。