I'm using HttpURLConnection
to perform a GET request to a specific URL. In some emulators and devices, it works perfectly and i'm getting 200 code, in others i'm getting 307 code. Can some one tell me what is the problem?
Here is my code:
URL cnx = new URL(url);
HttpURLConnection urlCon = (HttpURLConnection) cnx.openConnection(); urlCon.setRequestMethod("GET");
urlCon.setConnectTimeout((int) timeout);
urlCon.setReadTimeout((int) timeout);
urlCon.connect();
int code = urlCon.getResponseCode();
if (code != 200) {
return null;
}
Answer
I have resolved my problem by using DefaultHttpClient instead of HttpURLConnection. Here is the code if someone had the same issue:
public String WebServiceCall(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
res = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return res;
}
No comments:
Post a Comment