任何人都可以帮助我或建议一个很好的示例/教程,说明我将如何使用 JSON 和 httpost 将值发布到网站。我想要做的是将以下值提交到网站:
("DateObserved", _date);
("ProvinceId", "59");
("InformationTypeId", "3");
("ReportTypeId","3");
("IsCompetitor",_competitor);
("SubBrands","91");
("ConsumerSegments","3");
对于我作为 android 的新手来说,应用我在 SO 的不同帖子中读到的内容有点困难,所以我只想要求一个很好的例子或教程来帮助我理解它。无论如何,我已经为此尝试了一些方法,但我得到了 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();
}
日志猫:
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): 在 org.apache.http.impl.auth.BasicScheme.processChallenge(BasicScheme.java:95) 08-01 18:56:29.668: E/ClientServerDemo(2283): 在 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): 在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:478) 08-01 18:56:29.668: E/ClientServerDemo(2283) : 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 08-01 18:56:29.668: E/ClientServerDemo(2283): ... 10 更多