2014-09-06 190 views
0

我想发送一个JSON对象来检索一些数据。 JSON对象内部是从数据库中获取的年份,类型和国家/地区参数。但服务器似乎没有获得我传递的价值。从Android发送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

正常GET方法这是重复的问题和u可以读取这个教程更多[信息](http://hmkcode.com/android-send-json-data-to-server/ )和http://mongskiewl.wordpress.com/2013/10/16/sending-json-data-from-android-to-a-php-script/ – 2014-09-06 06:36:13

回答

0

看来我无法使用GET方法发送JSON,而我们E在端

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);