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:
on the distant server, the php code is:
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?
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:
