나는 mysql 서버에서 Google지도 용 좌표 쌍을 다운로드해야하는 애플리케이션을 사용하고 있습니다. 이 작업은 PHP와 일반 httpost를 사용하여 성공적으로 수행 할 수 있지만 앱을 종료하면 몇 초 동안 앱이 정지됩니다.비동기 HTTP 게시물 android
서버가 처리를 끝내고 결과를 보낼 때까지 동결을 방지하기 위해 httppost를 비동기로 만들어야한다고 읽었습니다.
요점은 정상적인 httpost와 같은 json 배열에서 그 결과가 필요하다는 것입니다.
예를 들어,이 httppost가있는 경우.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
String result11 = null;
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is11, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is11.close();
result11 = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parsing data
try {
JSONArray jArray1 = new JSONArray(result11);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
어떻게 내가 비동기 게시물로 변환하여 동결을 피할 수 있습니까?
난 당신의 코드도 그대로 컴파일하지만, ['EntityUtils.toString (response.getEntity())'(http://stackoverflow.com/a 이용하시기 바랍니다 수있는 방법을 몰라/2324739/180740)를 사용하는 것이 좋습니다. 전자는 코드가 적고 오류 처리가 올바르며 서버에서 보낸 문자 인코딩을 준수합니다. –