我正在尝试发送JSON对象来检索一些数据。 JSON对象内部是从数据库获取的年份,类型和国家/地区参数。但是服务器似乎没有得到我传递的价值。
这是我的Android代码:
public HttpResponse makeRequest(String uri, String json) {
try {
HttpUriRequest request = new HttpGet(uri);
request.addHeader("Accept-Encoding", "gzip");
request.addHeader("Content-type", "application/json");
return new DefaultHttpClient().execute(request);
} catch (UnsupportedEncodingException e) {
Log.d("tes", e.getMessage());
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d("tes", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.d("tes", e.getMessage());
e.printStackTrace();
} catch(Exception e){
Log.d("tes", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected JSONArray doInBackground(Void... params) {
Map<String, String> comment = new HashMap<String, String>();
comment.put("country", "Indonesia");
comment.put("year", "2014");
comment.put("type", "Audax");
String json = new GsonBuilder().create().toJson(comment, Map.class);
Log.d("sent json",json);
try {
HttpResponse response = makeRequest("http://racehub.me/mobile/native_races", json);
BufferedReader reader = null;
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
Log.d("tes", builder.toString());
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);
return finalResult;
} catch (IOException e) {
Log.d("fail", e.getMessage());
return null;
} catch (JSONException e) {
Log.d("fail", e.getMessage());
return null;
} catch(Exception e){
Log.d("tes", e.getMessage());
e.printStackTrace();
return null;
}
}
在logcat中,我看到发送的JSON(看起来没问题):
D/sent json﹕ {"type":"All","year":"2014","country":"All"}
这是接收JSON并从数据库返回结果的PHP
function native_races($f3) {
$table_series=new DB\SQL\Mapper($f3->get('DB'),'series');
$country = $f3->clean($f3->get('GET.country'));
$type = $f3->clean($f3->get('GET.type'));
$year = $f3->clean($f3->get('GET.year'));
$races = MainModel::getRaceList($f3, $country, $type, $year);
echo json_encode($races);
}
答案 0 :(得分:0)
看来我不能使用GET方法发送JSON,我最后使用普通的GET方法
String url = "http://racehub.me/mobile/native_races?";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("country", "all"));
nameValuePairs.add(new BasicNameValuePair("year", "2014"));
nameValuePairs.add(new BasicNameValuePair("type", "all"));
String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
url += paramString;
HttpUriRequest request = new HttpGet(url);
request.addHeader("Accept-Encoding", "gzip");
request.addHeader("Content-type", "application/json");
HttpResponse response = new DefaultHttpClient().execute(request);