2015-06-17 79 views

回答

0

是的,他們已被棄用。您可以使用Google Dev推薦的Volley

Volley提供以下好處: 自動調度網絡請求。 多個併發網絡連接。 標準HTTP緩存一致性的透明磁盤和內存響應緩存。 支持請求優先級。 取消請求API。您可以取消單個請求,也可以設置要取消的請求的範圍或範圍。 易於定製,例如,重試和退避。 強大的排序使得使用從網絡異步獲取的數據可以輕鬆正確地填充您的UI。

排球是非常容易使用:

// Instantiate the RequestQueue. 
RequestQueue queue = Volley.newRequestQueue(this); 
String url ="http://www.google.com"; 

// Request a string response from the provided URL. 
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
     new Response.Listener<String>() { 
@Override 
public void onResponse(String response) { 
    // Display the first 500 characters of the response string. 
    mTextView.setText("Response is: "+ response.substring(0,500)); 
} 
}, new Response.ErrorListener() { 
@Override 
public void onErrorResponse(VolleyError error) { 
    mTextView.setText("That didn't work!"); 
} 
}); 
// Add the request to the RequestQueue. 
queue.add(stringRequest); 
0

您可以使用HttpURLConnection

public String makeRequest(String pageURL, String params) 
{ 
    String result = null; 
    String finalURL =pageURL; 
    Logger.i("postURL", finalURL); 
    Logger.i("data", params); 
    try { 
     URL url = new URL(finalURL); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 
     urlConnection.setRequestProperty("Accept", "application/json"); 
     OutputStream os = urlConnection.getOutputStream(); 
     os.write(params.getBytes("UTF-8")); 
     os.close(); 
     int HttpResultCode =urlConnection.getResponseCode(); 
     if(HttpResultCode ==HttpURLConnection.HTTP_OK){ 
      InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
      result = convertStreamToString(in); 
      Logger.i("API POST RESPONSE",result); 
     }else{ 
      Logger.e("Error in response ", "HTTP Error Code "+HttpResultCode +" : "+urlConnection.getResponseMessage()); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 

然後你流轉換爲字符串

private String convertStreamToString(InputStream is) { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line).append('\n'); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

創建您的JSON

JSONObject j=new JSONObject(); 
     try { 
      j.put("name","hello"); 
      j.put("email","[email protected]"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    //Call the method 
     makeRequest("www.url.com",j.toString()); 
0

只是爲了展示另一種可能的庫,可以幫助你:OkHttp

,從他們的webseite一個例子後:

public static final MediaType JSON 
= MediaType.parse("application/json; charset=utf-8"); 

OkHttpClient client = new OkHttpClient(); 

String post(String url, String json) throws IOException { 
    RequestBody body = RequestBody.create(JSON, json); 
    Request request = new Request.Builder() 
     .url(url) 
     .post(body) 
     .build(); 
    Response response = client.newCall(request).execute(); 
    return response.body().string(); 
} 

Retrofit

你基本上創建包含其餘的API和參數你調用註釋的接口,可以接收解析JSON模式是這樣的:

public interface MyService { 
    @POST("/api") 
    void createTask(@Body CustomObject o, Callback<CustomObject > cb); 
} 

您還可以設置他們兩個在一起,這裏是一個指導,幫助我很多:https://futurestud.io/blog/retrofit-getting-started-and-android-client/

即使這不是谷歌文檔推薦的官方方式,這些都是不錯的庫,值得一看。

相關問題