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

Apps sending JSON to a server

Hi all

I am trying to send requests to a server in JSON format but my app die on the emulator when i try to send request. However the same code works fine as a normal java application.

Here is my code:
Code:
private HttpResponse doRequest(String url,JSONObject jso){
		HttpClient client = new DefaultHttpClient();
		HttpPost request = new HttpPost(url);
		HttpResponse response = null;
		try{
			StringEntity se = new StringEntity(jso.toString());
			se.setContentEncoding("UTF-8");
			se.setContentType("application/json");
			request.setEntity(se);
		}
		catch (UnsupportedEncodingException uee){
			
		}
		
		try {
			response = client.execute(request);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
		
	}

What wrong with this code if it run perfectly in a java application?

Thank you for your help!
 
Did you include the INTERNET permisson in your manifest file?
Did you include a modem in your AVD's supported hardware when it was created?

A log cat output would help.
 
I think jonbonazza is right... and maybe you forgot the internet permission.

More troubleshooting... echo out your request and response... you can then see what is more causing the issue.. client or server.

If you still are having issues please post some more code of what your trying to accomplish and I'll follow up.
 
Back
Top Bottom