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

Help No response on trying to post data to webservice

Rick Roy

Lurker
I am trying to post some data to my webservice but when I am testing it out in my device I am not getting error but at the same time nothing really happens, the tablet is responding just fine everything works but my webservice is not getting any data nor does the application show any error.

I have added some Toast messages to check the flow of logic and from what I can determine the app goes into the method to send data to the webservice and then just does nothing.

This is my code

//Method to post data to webservice

publicvoid post()throwsUnsupportedEncodingException{try{Toast.makeText(getBaseContext(),"Creating new user",Toast.LENGTH_SHORT).show();HttpURLConnection urlConnection =(HttpURLConnection)(new URL("http://www.rgbpallete.in/led/api/signup").openConnection());
urlConnection.setConnectTimeout(1500);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);

List<NameValuePair> params =newArrayList<NameValuePair>();
params.add(newBasicNameValuePair("uname", uname));
params.add(newBasicNameValuePair("pass", password));
params.add(newBasicNameValuePair("email", email));

OutputStream os = urlConnection.getOutputStream();BufferedWriter writer =newBufferedWriter(newOutputStreamWriter(os,"UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();

urlConnection.connect();if(urlConnection.getResponseCode()==200){InputStream inputStream =newBufferedInputStream(urlConnection.getInputStream());BufferedReader streamReader =newBufferedReader(newInputStreamReader(inputStream,"UTF-8"));StringBuilder responseStrBuilder =newStringBuilder();

String inputStr;while((inputStr = streamReader.readLine())!=null)
responseStrBuilder.append(inputStr);JSONObject json =newJSONObject(responseStrBuilder.toString());String message = json.getString("message");boolean error = json.getBoolean("error");

error(error, message);}

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

}

During testing the app executes this line Toast.makeText(getBaseContext(), "Creating new user", Toast.LENGTH_SHORT).show(); and shows "Creating new user" message and then nothing happens. My phone network is a bit faulty so may it be that it is taking a awfully long time to connect to my webservice or is my code faulty?

UPDATE

I tried a bit more debugging and found out that the code flow dies on the line

OutputStream os = urlConnection.getOutputStream();
Any one has any idea why?
 
Back
Top Bottom