【问题标题】:How to send more than 200 contacts to server in android?如何将 200 多个联系人发送到 android 中的服务器?
【发布时间】: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


【解决方案1】:

您发送的是 HTML 而不是 JSON。查看您的异常文本:

JSONException: Value <html><head><title>Apache of type java.lang.String cannot be converted to JSONObject

更新: 先生。 @ci_ - 绝对正确,可能真正的原因在于服务器端。由于某种原因(可能)它无法处理具有 200 多个联系人的 JSON。但是 OP 也没有提供任何信息(例如代码),我认为这里不可能回答清楚。

【讨论】:

  • 你在这里可能有点快。当然,问题中没有太多细节,但我相信您看到的错误是由于尝试将服务器返回的错误页面解析为 JSON。虽然这显然行不通,但我认为实际的问题是,为什么服务器会返回错误(400)? (这当然不能从问题的细节来回答)。
  • 是的,但低于 200 个联系人它会正确发送 JSON 超过 200 个联系人我遇到问题,它不会创建 json 对象。
猜你喜欢
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
相关资源
最近更新 更多