2016-03-08 86 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

相关问题