• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How could i do a request with POST method with parameters?

RaoulH

Newbie
Hello,

i'm a newbie android programmer.
In my appli, i use a request to contact a distant server. This server contains a php script.

On java side, this is the source code:

Java:
try {
            URL url = new URL(pUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty( "charset", "utf-8");
            String query ="";
            if(params.length!=0){
                Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("firstParam", (String) params[0])
                        .appendQueryParameter("secondParam", (String) params[1]);
                                query = builder.build().getEncodedQuery();
            }
            // Open connection for sending data
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            if(!query.equals("")){
                writer.write(query);
            }
            writer.flush();
            writer.close();
            os.close();
            conn.connect();
            int response_code = conn.getResponseCode();
            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
                StringBuilder sb = new StringBuilder();
                String line = null;
                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    sb.append(line);
                }
                reader.close();
                json = sb.toString();
            }else{
               Log.i("retour false", ""+response_code );
            }
        } catch (UnsupportedEncodingException e) {
           Log.e("", e.toString());
        } catch (MalformedURLException e) {
            Log.e("", e.toString());
        } catch (IOException e) {
            Log.e("", e.toString());
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
            Log.d("dd ", jObj.toString());
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e);
        }

on the distant server, the php code is:

PHP:
$lon = 0.0;
$lat= 0.0;
if (isset($_POST["firstParam"]) && isset($_POST["secondParam"]) ) {
       $lon=$_POST["firstParam"];
    $lat= $_POST["secondParam"];
}
$response["lon"] = utf8_encode($lon);
$response["lat"] = utf8_encode($lat);
********
********
echo json_encode($response,JSON_UNESCAPED_UNICODE);

When i launch my appli, the url request returns only an error 500 and i didn't have any other errors.

Please could you help me?
 
Last edited:
Application Development thread moved to the Development forum for better exposure.:)
 
ok, i use fiddler 4 to debug when i launch the request.
i got this warning : "illegal/unquoted identifier 'firstParam' at position 0." and the error 500.
if i launch my request without params, it works correctly!!!.
I didn't have any other errors.
 
So your next step would be to investigate what parameter values are being sent with the request. In particular, I'd be looking at the value for params[0]
 
Last edited by a moderator:
finally, i resolved the issue with the error 500, in my host (It is OVH), i configure the .ovhconfig file to "development" environment instead of "production" (by default).
Now, the request returns the status 200 but it's very strange because the values of $lon and $lat are 0 (zero) instead of values sent in the request.
When i launch my request with postman, it is ok, it returns the correct values but when i launch it in my appli, it returns 0. In each case, the status of the request is 200.

Please could you tell me if my source code in Java was ok? maybe i forgot some things.

Thanks in advance,
 
Hello,

finally, after checking closely the source code java, i found that this instruction should be comment:
Java:
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
Now it is ok.
Thanks to LV426 for his help.
 
Back
Top Bottom