Rick Roy
- Forum posts: 1
Dec 20, 2014, 6:09:42 AM via Website
Dec 20, 2014 6:09:42 AM via Website
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
public void post() throws UnsupportedEncodingException
{
try {
Toast.makeText(getBaseContext(), "Creating new user", Toast.LENGTH_SHORT).show();
HttpURLConnection urlConnection = (HttpURLConnection) (new URL("some url that the forum wont let me post").openConnection());
urlConnection.setConnectTimeout(1500);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uname", uname));
params.add(new BasicNameValuePair("pass", password));
params.add(new BasicNameValuePair("email", email));
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
urlConnection.connect();
if(urlConnection.getResponseCode() == 200){
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
JSONObject json = new JSONObject(responseStrBuilder.toString());
String message = json.getString("message");
boolean error = json.getBoolean("error");
error(error, message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
On execution I have found out that the flow dies at
OutputStream os = urlConnection.getOutputStream();
I get no error, no connection is ever established with my webserver.
Why is this happening?
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.