如何在android中向服务器发送200多个联系人?

时间:2015-05-12 14:09:30

标签: android json android-asynctask android-contacts

我使用cursor从我的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)

1 个答案:

答案 0 :(得分:2)

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

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

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