2017-04-18 84 views
0

HttpUrlConnection POST請求不起作用。告訴我是否有任何其他方式在android中發出POST請求。如果有任何其他方式在android中發出POST請求,請告訴我。如果有任何其他方式在android中發出POST請求,請告訴我。HttpUrlConnection POST請求不能正常工作....如何解決此問題?

public final String apiCall(String pUrl) { 
    if(! isInternetAvailable()) 
     return "NO_INTERNET"; 

    try { 
     URL lUrl = new URL(pUrl.replace(" ", "%20")); 
     Log.i("url", String.valueOf(lUrl)); 

     String url = pUrl; 

     Log.i("dom", url.substring(0, (url.indexOf('?') - 1))); 
     Log.i("para", url.substring((url.indexOf('?') + 1), url.length())); 

     URL obj = new URL(url.substring(0,(url.indexOf('?')-1))); 
     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

     //add reuqest header 
     con.setRequestMethod("POST"); 
     con.setRequestProperty("User-Agent", "GYUserAgentAndroid"); 
     con.setRequestProperty("Content-Type", "application/json"); 

     String urlParameters = url.substring((url.indexOf('?')+1), url.length()); 
     Log.i("urlParameters", urlParameters.toString()); 

     // Send post request 
     con.setDoInput(true); // true if we want to read server's response 
     con.setDoOutput(true); // false indicates this is a GET request 
     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     wr.writeBytes(urlParameters); 
     wr.flush(); 
     wr.close(); 

     int responseCode = con.getResponseCode(); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 


     Log.i("res",response.toString()); 
     return response.toString(); 

    }catch (Exception e) { 
     Log.i("secondEx",e.toString()); 
     return "ERROR"; 
    } 

} 
+0

我建議你使用Retrofit - 它是一個類型安全的Android和Java的HTTP客戶端。 在這裏你可以學習這個 http://square.github.io/retrofit/ –

回答

0

試試這個

InputStream inputStream; 
HttpURLConnection urlConnection; 
byte[] outputBytes; 

public class WebServiceAsyncTask extends AsyncTask<Void, Void, String> { 

    @Override 
    protected String doInBackground(Void... params) { 

     try { 
      URL url = new URL(Url); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      outputBytes = query.getBytes("UTF-8"); 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoOutput(true); 
      urlConnection.setConnectTimeout(15000); 
      urlConnection.connect(); 

      OutputStream os = urlConnection.getOutputStream(); 
      os.write(outputBytes); 
      os.flush(); 
      os.close(); 

      inputStream = new BufferedInputStream(urlConnection.getInputStream()); 
      ResponseData = convertStreamToString(inputStream); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 

     } 
     return ResponseData; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 


    } 

    public 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 + "\n")); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 

傳遞你的參數以JSON字符串格式這樣

JSONObject() params = new JSONObject(); 
params.put("key","your parameter"); 
params.put("key","your parameter"); 

query=params.toString(); 

檢查this鏈接,參考

+0

outputBytes?查詢? –

+0

查詢是您發送給後期api的參數。它是在JSON字符串格式 – AbhayBohra

+0

@surajshinde檢查我的編輯 – AbhayBohra

0

執行請求的另一種方式是Retrofit

你可以找到一個很好的教程here