public class HttpPosrHitter {
public static String getJSONfromURL(String url, String member_id,
String phonenumber) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("memberid", member_id));
pairs.add(new BasicNameValuePair("numbers", phonenumber));
httppost.setEntity(new UrlEncodedFormEntity(pairs));
// http post
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return result;
}
}
这是我从中将电话号码发布到Web服务并获得响应的课程。
当我发布具有15到20个联系方式的电话号码时,我得到答复。但是,当我发布有150个联系人的电话号码时,一次却没有得到一个响应,我必须重新启动两次应用程序,然后我才得到响应。我不知道我在哪里做错。即使我无法读取具有固定大小缓冲区的块中的电话大文件。
最佳答案
只需一次解决所有潜在的错误:是否有任何阻止您使用Retrofit和GSON或Jackson的错误?
每当我看到这样的JSON / InputStream / URLConnection / ...问题时,我总是想知道为什么人们总是花时间重新发明基本内容,而不是实际编写应用程序。