2015-10-11 50 views
1

好吧我可能有理解okhttp的問題。我需要的只是將圖像上傳到服務器。okhttp替代容器

enter image description here

這個形象是從我的機器人工作室的截圖,看來我不能import org.apache.http.NameValuePair;什麼是另類容器呢?

,當你注意到.open(url)也紅了跟它cannot resolve java.net.url

這是我的全碼:

public JSONObject getJSONFromUrl(String str_url, List<NameValuePair> params) { 
    String reply_str = null; 
    BufferedReader reader = null; 
    final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("image",image_str)); 
    nameValuePairs.add(new BasicNameValuePair("caption",caption)); 
    nameValuePairs.add(new BasicNameValuePair("categorie",categorie)); 
    try { 
     URL url = new URL(str_url); 
     OkHttpClient client = new OkHttpClient(); 
     HttpURLConnection con = client.open(url); 
     con.setDoOutput(true); 
     OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); 
     writer.write(getEncodedParams(params)); 
     writer.flush(); 
     StringBuilder sb = new StringBuilder(); 
     reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     reply_str = sb.toString(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 

    // try parse the string to a JSON object. There are better ways to parse data. 
    JSONObject jObj; 
    try { 
     jObj = new JSONObject(reply_str); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 
    return jObj; 
} 

//in this case it's NameValuePair, but you can use any container 
public String getEncodedParams(List<NameValuePair> params) { 
    StringBuilder sb = new StringBuilder(); 
    for (NameValuePair nvp : params) { 
     String key = nvp.getName(); 
     String param_value = nvp.getValue(); 
     String value = null; 
     try { 
      value = URLEncoder.encode(param_value, "UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 

     if (sb.length() > 0) { 
      sb.append("&"); 
     } 
     sb.append(key + "=" + value); 
    } 
    return sb.toString(); 
} 
+0

改爲使用HashMap或JSONObject – BNK

回答

3

,而不是BasicNameValuePair可以使用Pair

相當於爲getKey()會是pair.first。一對的getValue()將pair.second

+0

client.open(url)怎麼辦?爲什麼我得到錯誤 – Moudiz

+0

'OkHttpClient'沒有叫'open'的成員。這個'open'方法應該返回一個HttpUrlConnection對象也很奇怪。 – Blackbelt

2

建議大家參閱的NameValuePair替代如下:

... 
      OkHttpClient client = new OkHttpClient(); 
      JSONObject jsonObject = new JSONObject(); 
      try { 
       jsonObject.put("key1", "value1"); 
       jsonObject.put("key2", "value2"); 
       jsonObject.put("key3", "value3"); 
       RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString()); 
       Request request = new Request.Builder() 
         .url("http://...") 
         .post(body) 
         .build(); 
       client.newCall(request).enqueue(new Callback() { 
        @Override 
        public void onFailure(Request request, IOException e) { 
         // do something... 
        } 
        @Override 
        public void onResponse(Response response) throws IOException { 
         // do something... 
        } 
       }); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

希望這有助於!

+0

謝謝你生病檢查它btw你開始使用okhttp? – Moudiz

+0

是的,只是爲了一些小測試項目:) – BNK

+1

哦,這是好人好運,謝謝你幫助我。我在我的兼職工作機器人和它令人討厭的bcz我在數據庫(甲骨文)工作,但我試圖提高我的自我與機器人當我有空閒時間 – Moudiz