Reputation: 39
I was using AsyncHttpClient to make some requests to my database and It was working fine. But, when I tried to use 3g on my mobile, it simply didn't work. That's why I decided to change HttpClient to HttpUrlConnection (people said that it works better with 3g + wifi). But, when I was coding it, I just got stuck at a buffer problem that I can't solve. The code is:
public class SendPostRequest extends AsyncTask<String, Void, String>{
String base64CredenciaisCodificadas;
HashMap postDataParam;
@Override
protected String doInBackground(String... url) {
String response = "";
try{
URL link = new URL(url[0]);
HttpURLConnection e = (HttpURLConnection)link.openConnection();
e.setReadTimeout(15000);
e.setConnectTimeout(15000);
e.setRequestMethod("POST");
e.setRequestProperty("Authorization", "Basic " + base64CredenciaisCodificadas);
e.setDoInput(true);
e.setDoOutput(true);
e.setFixedLengthStreamingMode(getPostDataString(postDataParam).length());
OutputStream os = e.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParam));
writer.flush();
writer.close();
os.close();
int responseCode = e.getResponseCode();
if (responseCode == 200){
//Code to get data from web service.
}
} catch(Exception e){
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator var4 = params.entrySet().iterator();
while(var4.hasNext()) {
Map.Entry entry = (Map.Entry)var4.next();
if(first) {
first = false;
result.append("?");
} else {
result.append("&");
}
result.append(URLEncoder.encode((String)entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode((String)entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
And the error log is:
10-25 15:16:11.128 30807-30898/petma.testesappcarona I/System.out: Stat: 500; Msg: buffer(com.android.okhttp.internal.http.HttpConnection$UnknownLengthSource@834db8).inputStream()
If anyone has any more doubts, please just ask and I will send additional data.
Edit: It is android. It has permission to internet at manifest.
Upvotes: 1
Views: 544
Reputation: 39
I managed the problem. It wasn't problem at the code. The web service I was trying to connect was only accessible through GET request. I switched the POST to GET and it now works!
But, there is now a new Problem: When switching to 3g, I get 403: Access forbidden. Any ideas?
Upvotes: 0
Reputation: 103
Common reason: Steam is getting closed either by wrong parameters or by other unhandled exception.
Possible cause: HashMap postDataParam is never assigned in this code, due to which you are passing null in below code.
e.setFixedLengthStreamingMode(
getPostDataString(postDataParam).length());
And null parameter is causing com.android.okhttp.internal.http.HttpConnection$UnknownLengthSource@834db8).inputStream()
Free advice :
Try to reuse the code wherever is possible example
you could have stored getPostDataString(postDataParam) in some variable since its getting used twice in your code.
Upvotes: 1