2016-03-08 88 views
-1

我在我的Android應用程序中使用HttpPost類的方法。 它與目標sdk 4.4.2正常工作,但我做了一些改變,並使目標sdk到23(6.0)。 現在HttpPost類發生錯誤。 我也讀過關於HttpUrlConnection,但不知道如何使用它。 這裏是我的代碼HttpPost在API 23中給出錯誤android

private String getJSON(String URL, JSONObject obj) throws JSONException { 
     String responseString = null; 
     try { 

      HttpPost httppost = new HttpPost(URL); 
      StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8); 
      httppost.setHeader("Content-Type", "application/json"); 

      httppost.setEntity(se); 

      HttpParams httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParams, 30000); 
      HttpClient httpclient = new DefaultHttpClient(httpParams); 
      HttpResponse httpResponse = httpclient.execute(httppost); 

      StatusLine statusLine = httpResponse.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      System.out.println("status code is" + statusCode); 
      HttpEntity entity = httpResponse.getEntity(); 
      responseString = EntityUtils.toString(entity, "UTF-8"); 
      System.out.println("response string" + responseString); 
      Log.i("RESPONSE XML ------> ", "-----> " + responseString); 
      return responseString; 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return responseString; 
    } 

請幫助我能做些什麼,或者如果可能提供的類此功能,將提前

+0

你得到什麼錯誤? – Exception

+0

我剛剛得到不能解決類型錯誤。並且沒有選擇導入任何東西,但只要我將目標sdk更改爲4.4.2就可以正常工作。 –

+0

你可以發佈堆棧跟蹤嗎?所以它可以更清楚。 – Talha

回答

1

下面給出的代碼替換裏面的功能代碼第23 由於工作,使用HttpUrlConnection

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import org.json.JSONException; 
import org.json.JSONObject; 

public class TestJava { 

private String getJSON(String URL, JSONObject obj) throws JSONException { 
    URL url; 
    HttpURLConnection connection = null; 
    try { 
     //Create connection 
     url = new URL(URL); 
     connection = (HttpURLConnection)url.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", 
       "application/json"); 
     connection.setUseCaches (false); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setConnectTimeout(30000); 
     //Send request 
     DataOutputStream wr = new DataOutputStream (
       connection.getOutputStream()); 
     wr.write(obj.toString().getBytes("UTF-8")); 
     wr.flush(); 
     wr.close(); 

     //Get Response 
     InputStream is = connection.getInputStream(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     String line; 
     StringBuffer response = new StringBuffer(); 
     while((line = rd.readLine()) != null) { 
      response.append(line); 
      response.append('\r'); 
     } 
     rd.close(); 
     return response.toString(); 

    } catch (Exception e) { 

     e.printStackTrace(); 
     return null; 

    } finally { 

     if(connection != null) { 
      connection.disconnect(); 
     } 
    } 

} 
} 
+0

這段代碼不工作我試過了,這仍然是讓我進入錯誤塊 –

+0

哪條線路出現錯誤? – Exception

+0

@raviraghav,添加這些導入語句,如果你還沒有添加它。 'import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;' – Exception

相關問題