2014-09-23 71 views
0

我試圖在android.c中實現分頁。我現在有一個返回jsonarray的服務。Android如何使用參數調用Web服務並獲取jsonarray作爲響應

protected JSONArray doInBackground(String... params) { 

    JSONArray jsonArray = null; 
    String result = ServerConnect.readService("http://myservice.com/resbook_redberry/mobile/GetKitchenOrder"); 

    try { 
     jsonArray = new JSONArray(result); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     try { 
      jsonArray = new JSONArray(result.substring(3)); 
     } catch (JSONException e1) { 
      e1.printStackTrace(); 
     } catch (Exception e2) { 
      e2.printStackTrace(); 
     } 
    } 
    return jsonArray; 
} 

,但現在我需要發送的頁碼到Web服務,並得到JSONArray作爲響應。


我需要寫HTTP後?爲了做到這一點,我應該改變什麼?

回答

3
Try this: here i am using gson library to parse json : https://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.4-release.zip&can=1&q= 

**With GET Method** 

    protected SignUpResponseJson doInBackground(String... params) { 

     urlString = "http://www.xxxxx.com?page=1" 
     url = new URL(urlString); 

     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setUseCaches(false); 
     connection.setConnectTimeout(Integer.parseInt(context.getResources().getString(R.string.timeout))); //set timeout to 5 seconds 
     connection.setReadTimeout(Integer.parseInt(context.getResources().getString(R.string.timeout))); //set timeout to 5 seconds 

     connection.connect(); 

     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     int responseCode = httpConnection.getResponseCode(); 
     //  Log.i("test-responsecode",String.valueOf(responseCode)); 
     if (responseCode == HttpURLConnection.HTTP_OK) { 
      //   Log.i("test","inside if"); 
      InputStream in = httpConnection.getInputStream(); 
      Gson gson = new Gson(); 
      Reader r = new InputStreamReader(in); 

      Type DataType = new TypeToken<SignUpResponseJson>() { 
      }.getType(); 

      signUpResponseJsons = gson.fromJson(r,DataType); 
      Log.i(TAG, gson.toJson(signUpResponseJsons).toString()); 

     } 

    } 

或POST方法

http://fahmirahman.wordpress.com/2011/04/26/the-simplest-way-to-post-parameters-between-android-and-php/

public void postData(){ 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.yourdomain.com/post.php"); 

     try { 
      // Add your data 
      List nameValuePairs = new ArrayList(1); 
      nameValuePairs.add(new BasicNameValuePair("data1", "dataValue")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      InputStream is = response.getEntity().getContent(); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 
} 
+0

感謝您的answer..but我需要的是發佈頁號到服務器,並獲得JSON數組根據該請求。在獲取json之前,您是否將任何值發佈到服務器? – san88 2014-09-23 08:23:05

+0

是的,urlString =「http://www.xxxxx.com?page=1」你可以在url中加入查詢字符串 – 2014-09-23 08:26:36

+0

yes..但不幸的是它不接受通過url的參數。有沒有選擇發送參數給服務器以外的url? – san88 2014-09-23 08:37:17

相關問題