Android from (HTTP) POST to GET



我正在工作2相同的应用程序需要发送到一个php文件相同的字符串。当我在PHP中使用这段代码来检查它时,iOs的一个是GET。

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

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

这是我的android代码:

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

变化:

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

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

如果你得到networkonmainthreadeexception,这是因为你试图从主线程做网络操作。如果是,请尝试查看AsyncTask。

还要确保你在android manifest中有上网权限

最新更新