Dro*_*man 7 android json http-post
编辑完全重新解决了问题,以便更好地理解
我必须http://api.bf3stats.com/pc/player/使用2个POST参数查询给定的URL :'player'(对于玩家名称)和'opt'(对于选项).我已经在http://www.requestmaker.com/上测试了以下数据:player=Zer0conf&opt=all.我得到了正确的JSON响应(我以为我不知道他们的网站如何执行查询,我猜它是php).现在我想在Android中做同样的事情:
private StringBuilder inputStreamToString(InputStream is) {
//this method converts an inputStream to String representation
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
这就是我提出请求的方式:
public void postData(String url, String name) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//qname is a String containing a correct player name
nameValuePairs.add(new BasicNameValuePair("player", qname));
nameValuePairs.add(new BasicNameValuePair("opt", "all"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
//test is just a string to check the result, which should be in JSON format
test = inputStreamToString(response.getEntity().getContent())
.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
Run Code Online (Sandbox Code Playgroud)
我在'test'中得到的字符串不是JSON,而是一些bf3stats页面的完整HTML标记.我的请求有什么问题?

N-J*_*JOY 10
您需要"application/x-www-form-urlencoded"在请求标头中设置要发送的数据类型的内容类型.
我为上面提到的数据测试了你的API,它工作正常,我收到了大量的数据作为回应.你可以在这里找到它.
您可以尝试以下代码:
public String postDataToServer(String url) throws Throwable
{
HttpPost request = new HttpPost(url);
StringBuilder sb=new StringBuilder();
String requestData = prepareRequest();
StringEntity entity = new StringEntity(requestData);
entity.setContentType("application/x-www-form-urlencoded;charset=UTF-8");//text/plain;charset=UTF-8
request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
request.setHeader("Accept", "application/json");
request.setEntity(entity);
// Send request to WCF service
HttpResponse response =null;
DefaultHttpClient httpClient = new DefaultHttpClient();
//DefaultHttpClient httpClient = getNewHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10*1000);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),10*1000);
try{
response = httpClient.execute(request);
}
catch(SocketException se)
{
Log.e("SocketException", se+"");
throw se;
}
/* Patch to prevent user from hitting the request twice by clicking
* the view [button, link etc] twice.
*/
finally{
}
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
}
Log.d("response in Post method", sb.toString()+"");
return sb.toString();
}
public String prepareRequest()
{
return "player=Zer0conf&opt=all";
}
Run Code Online (Sandbox Code Playgroud)
编辑: 我收到错误期望失败错误 - 417为您的Web服务.所以我们需要在我们的请求中添加一个参数.是的request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);.我用这个更新了上面的代码.现在它工作正常.
响应看起来像这样.
希望这会有所帮助.
干杯
N_JOY
| 归档时间: |
|
| 查看次数: |
10710 次 |
| 最近记录: |