【发布时间】:2015-05-12 14:09:30
【问题描述】:
我正在使用光标从我的 android 设备获取联系人。成功获取所有联系人。如果光标计数低于 200,它会成功保存服务器,否则它会崩溃。
我正在准备所有联系人的 json 数组并发送到服务器但超过 200 个联系人我正在划分联系人并发送到服务器但它不接受它返回 400 错误代码。下面是我的日志猫。(我一次向服务器发送 100 个联系人)
05-12 18:35:11.170: I/Timeline(18091): Timeline: Activity_idle id: android.os.BinderProxy@15a30238 time:1943759
05-12 18:35:15.370: W/System.err(18091): org.json.JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject
05-12 18:35:15.370: W/System.err(18091): at org.json.JSON.typeMismatch(JSON.java:111)
05-12 18:35:15.370: W/System.err(18091): at org.json.JSONObject.<init>(JSONObject.java:160)
05-12 18:35:15.370: W/System.err(18091): at org.json.JSONObject.<init>(JSONObject.java:173)
05-12 18:35:15.370: W/System.err(18091): at com.example.gettingcontacts.MainActivity$DoBack_process.onPostExecute(MainActivity.java:194)
05-12 18:35:15.370: W/System.err(18091): at com.example.gettingcontacts.MainActivity$DoBack_process.onPostExecute(MainActivity.java:1)
05-12 18:35:15.370: W/System.err(18091): at android.os.AsyncTask.finish(AsyncTask.java:632)
05-12 18:35:15.370: W/System.err(18091): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-12 18:35:15.370: W/System.err(18091): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-12 18:35:15.370: W/System.err(18091): at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 18:35:15.370: W/System.err(18091): at android.os.Looper.loop(Looper.java:145)
05-12 18:35:15.370: W/System.err(18091): at android.app.ActivityThread.main(ActivityThread.java:5942)
05-12 18:35:15.370: W/System.err(18091): at java.lang.reflect.Method.invoke(Native Method)
05-12 18:35:15.370: W/System.err(18091): at java.lang.reflect.Method.invoke(Method.java:372)
05-12 18:35:15.370: W/System.err(18091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
05-12 18:35:15.370: W/System.err(18091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
提前致谢。
下面是我的示例代码
HttpPost request = new HttpPost("Server URL");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("user-agent", "Yoda");
try {
myjson = new JSONObject();
myjson.put("userID", 15);
myjson.put("contacts", finalJarray);
//Log.e("json request is","request is "+myjson.toString());
StringEntity entity = new StringEntity(myjson.toString());
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json; charset=utf-8"));
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(request);
int resCode = response.getStatusLine().getStatusCode();
//InputStream instream = response.getEntity().getContent();
//tempresult = convertStreamToString(instream);
//instream.close();
resultcontact = EntityUtils.toString(response.getEntity());
//Log.e("resultent value is ", "result value " + result);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
jobj= new JSONObject(resultcontact);(this line i am getting the error)
String message=jobj.getString("message");
}
catch (JSONException e) {
// TODO Auto-generated catch block
Log.e("login activity","...do back catch block....");
e.printStackTrace();
}
05-12 20:35:13.416: D/MainActivity(10734): 我要发送的字符串 - Apache Tomcat/7.0.52 (Ubuntu) - 错误报告
HTTP 状态 400 -
类型状态报告
消息
说明客户端发送的请求语法错误。
Apache Tomcat/7.0.52 (Ubuntu)
【问题讨论】:
-
可能因为
Value Apache of type java.lang.String cannot be converted to JSONObject,你的JSON格式不正确,我推荐你这个JSON验证器:jsonlint.com -
没有代码我们无法理解问题。
-
@Tofasio 感谢您的回复,我的 json 很好,为什么因为低于 200 个联系人它成功保存服务器不抛出任何异常。
-
@hegazy 给我您的邮件 ID,我正在向您的邮件发送样本
-
@Ashok 不要使用
AsyncTasck进行长时间操作:)
标签: android json android-asynctask android-contacts