在Android中发布来自HttpPost的值

时间:2013-07-31 10:55:53

标签: android http-post

任何人都可以帮助我或建议一个很好的示例/教程,我将如何使用JSON和httpost将值发布到网站。我想要做的是将以下值提交到网站:

("DateObserved", _date);
("ProvinceId", "59");
("InformationTypeId", "3");
("ReportTypeId","3");
("IsCompetitor",_competitor);
("SubBrands","91");
("ConsumerSegments","3");

对我来说有点难以作为Android中的新手应用我在不同帖子中阅读的内容所以我只是想要一个好的例子或教程,可以帮助我理解它。无论如何,我已经为此尝试了一些东西,但我得到了ClientProtocolException。我真的不知道我错过了什么。请帮帮我......

HttpParams httpParameters = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);           

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost("http://svr4.sampleserver.com:0727/api/reports");



        JSONObject report = new JSONObject();
        report.put("IsCompetitor", _competitor);
        report.put("DateObserved", _date);
        report.put("ProvinceId", "59");
        report.put("InformationTypeId", "3");
        report.put("ReportTypeId","3");
        report.put("IsCompetitor",_competitor);
        report.put("SubBrands","91");
        report.put("ConsumerSegments","3");



         StringEntity entity1 = new StringEntity(report.toString(), HTTP.UTF_8);
         entity1.setContentType("application/json");
         httpPost.setEntity(entity1);

         httpPost.setHeader("Content-type", "application/json");
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();

         Log.e("ReportCode",_competitor); 
           // in.close();

    }catch (Exception e){
    Log.e("ClientServerDemo", "Error:", e);
    exception = e;
    //Toast.makeText(getApplicationContext(), e.getMessage().toString(), 5).show();
}

logcat的:

 08-01 18:56:29.668: E/ClientServerDemo(2283): org.apache.http.client.ClientProtocolException
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at   org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at com.undfgfgdfb.ireport.DoPost.doInBackground(DoPost.java:102)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at com.undfgfgdfb.ireport.DoPost.doInBackground(DoPost.java:1)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at android.os.AsyncTask$2.call(AsyncTask.java:252)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 08-01 18:56:29.668: E/ClientServerDemo(2283):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
08-01 18:56:29.668: E/ClientServerDemo(2283):   at java.lang.Thread.run(Thread.java:1020)
08-01 18:56:29.668: E/ClientServerDemo(2283): Caused by: org.apache.http.auth.MalformedChallengeException: Authentication challenge is empty
08-01 18:56:29.668: E/ClientServerDemo(2283):   at org.apache.http.impl.auth.RFC2617Scheme.parseChallenge(RFC2617Scheme.java:72)

08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.auth.AuthSchemeBase.processChallenge(AuthSchemeBase.java:111)     08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.auth.BasicScheme.processChallenge(BasicScheme.java:95)     08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.client.DefaultRequestDirector.processChallenges(DefaultRequestDirector.java:1071)     08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:968)     08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:478)     08-01 18:56:29.668:E / ClientServerDemo(2283):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)     08-01 18:56:29.668:E / ClientServerDemo(2283):... 10 more

1 个答案:

答案 0 :(得分:0)

我做了以下对我有用的事情,

String  responseBody = null;
        HttpPost post = new HttpPost("http://XXXXXXXXX");
        String udid = "XXXXX"; 
        String device_token = "********"; 
        JSONObject jObj = new JSONObject(); 
        try {
        jObj.put("udid", udid);
            jObj.put("device_token", device_token);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }         
        StringEntity se = null;
        try {
            se = new StringEntity(jObj.toString());
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        post.setEntity(se); 
        post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setHeader("Content-type", "application/json"); 
        ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
        HttpClient client = new DefaultHttpClient();
        try {
              responseBody = client.execute(post, responseHandler);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return responseBody;