Android JSON HttpClient to send data to PHP server with HttpResponse

后端 未结 5 760
野性不改
野性不改 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:24
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");
    request.setEntity(s);
    
    0 讨论(0)
  • 2020-11-29 00:31

    After lots of reading and searching I have found the problem to be with, I beleive magic_quotes_gpc being enabled on the server.

    Thus, using:

    json_decode(stripslashes($_POST['vehicle']));
    

    In my example above removes the slashes and allows the JSON to be decoded properly.

    Still not sure why sending a StringEntity causes a 403 error?

    0 讨论(0)
  • 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) {
    }
    

    }

    0 讨论(0)
  • 2020-11-29 00:40

    Try this code it works perfectly

    *For HttpClient class* download jar file "httpclient-4.3.6.jar" and put in libs folder then
    Compile:   dependencies {compile files('libs/httpclient-4.3.6.jar')}
    
    repositories {
            maven {
                url "https://jitpack.io"
            }
        }

    then call HttpClient class this AsyncTask Like this:

    private class YourTask extends AsyncTask { private String error_msg = "Server error!";

        private JSONObject response;
    
    
    
        @Override
        protected Boolean doInBackground(String... params) {
            try {
                JSONObject mJsonObject = new JSONObject();
                mJsonObject.put("user_id", "user name");
                mJsonObject.put("password", "123456");
                String URL=" Your Link"
    
                //Log.e("Send Obj:", mJsonObject.toString());
    
                response = HttpClient.SendHttpPost(URL, mJsonObject);
                boolean status = response != null && response.getInt("is_error") == 0; // response
    
                return status;
            } catch (JSONException | NullPointerException e) {
                e.printStackTrace();
                mDialog.dismiss();
                return false;
            }
        }
    
        @Override
        protected void onPostExecute(Boolean status) {
           // your code
    
        }
    }
    
    0 讨论(0)
  • 2020-11-29 00:41

    Change

    (String url = "http://www.server.com/MainPage.php";)
    

    to

    (String url = "http://www.server.com/MainPage.php?";)
    

    Question mark at the end is necessary when you're trying to send parameters to php script.

    0 讨论(0)
提交回复
热议问题