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

Apps Adding JSON to HTTPPost - Help Pls

Hi all,

I'm trying to access a web service by "Post"-ing a valid email and password.
It works in google chrome, but I have not gotten it to work in Android.
Below is my attempt to access the web service but I get a

{"status":"500","error":"Internal Server Error"}

error.

Any help would be appreciated...

[HIGH]

class PostToUnilabLogin extends AsyncTask<String, Integer, String>{

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub


String url = "http://xxx-edetailing.loudcloud.ph/api/v1/login.json";


JSONObject jsonObject = new JSONObject();
try{
jsonObject.put("password", password);
jsonObject.put("email", username);


}catch(JSONException e){
e.printStackTrace();
}


try{

//defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json");

httpPost.setHeader("Content-Type", "application/json");


StringEntity se = new StringEntity(jsonObject.toString());

Log.d("xxx", jsonObject.toString());

se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);


HttpResponse httpResponse = httpClient.execute(httpPost);

//Log.d("JPC", EntityUtils.toString(httpResponse.getEntity()));

HttpEntity httpEntity = httpResponse.getEntity();

//Log.d("JPC", EntityUtils.toString(httpEntity));

is = httpEntity.getContent();



}catch(Exception e){
e.printStackTrace();
}

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();

Log.d("xxx", json);

}catch(Exception e){


}




return null;
}

@Override
protected void onPostExecute(String result){
Log.d("xxx", "onPostExecute");
}

}

[/HIGH]
 
Error 500 means a problem on the server side. The server does not understand or able to preform your POST request. So probably something is wrong with the way you build/encode your request
 
Try to comment out all places you set header except for this one:

Code:
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

Let me know if it now works.. if not I will take another look. :)
 
Back
Top Bottom