Android JSON HttpClient to send data to PHP server with HttpResponse

后端 未结 5 779
野性不改
野性不改 2020-11-28 23:53

I am currently trying to send some data from and Android application to a php server (both are controlled by me).

There is alot of data collected on a form in the a

5条回答
  •  自闭症患者
    2020-11-29 00:32

    Try this code it works for me

    public void postData(String result,JSONObject obj) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    
    String json=obj.toString();
    
    try {
    
        HttpPost httppost = new HttpPost(result.toString());
        httppost.setHeader("Content-type", "application/json");
    
        StringEntity se = new StringEntity(obj.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 
    
        HttpResponse response = httpclient.execute(httppost);
        String temp = EntityUtils.toString(response.getEntity());
        Log.i("tag", temp);
    
    
    } catch (ClientProtocolException e) {
    
    } catch (IOException e) {
    }
    

    }

提交回复
热议问题