2015-04-06 91 views
1

我想要做的是从HttpURLConnection获取一些数据,而不是使用Volley库中的JsonObjectRequest()如何使用HttpURLConnection而不是Volley获取JSON对象?

下面是我用来从我的服务器获取JSON对象的代码。

JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,"myURL", null, new Response.Listener<JSONObject>(); 

我试图改变nullJSONObjectmyURL后改为null。但那并没有奏效。

回答

15

JsonObjectRequest()中的url不是可选的,并且JSONObject参数用于发送带有对url的请求的参数。

从文档: http://afzaln.com/volley/com/android/volley/toolbox/JsonObjectRequest.html

http://developer.android.com/training/volley/index.html

JsonObjectRequest

公共JsonObjectRequest(INT方法, 字符串URL, 的JSONObject jsonRequest, Response.Listener听众, Response.ErrorListener errorListener)创建一个新的请求。

参数:

方法 - URL从

jsonRequest获取JSON - - 一个JSONObject发布与请求使用

URL的HTTP方法。空允许为 并指示没有参数将与请求一起发布。

监听器 - 监听器接收JSON响应

errorListener - 错误监听器,或者为null,忽略错误。

使用HttpURLConnection

http://developer.android.com/reference/java/net/HttpURLConnection.html

的代码将是这样的:

public class getData extends AsyncTask<String, String, String> { 

     HttpURLConnection urlConnection; 

     @Override 
     protected String doInBackground(String... args) { 

      StringBuilder result = new StringBuilder(); 

      try { 
       URL url = new URL("https://api.github.com/users/dmnugent80/repos"); 
       urlConnection = (HttpURLConnection) url.openConnection(); 
       InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

       String line; 
       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 

      }catch(Exception e) { 
       e.printStackTrace(); 
      } 
      finally { 
       urlConnection.disconnect(); 
      } 


      return result.toString(); 
     } 

     @Override 
     protected void onPostExecute(String result) { 

      //Do something with the JSON string 

     } 

    } 
+0

那么有没有一种方法,我可以从HTTP URL连接获取JSON而不是一个URL?谢谢 – 2015-04-06 06:24:17

+1

@AssassinShadow刚刚更新了我刚刚得到工作和测试的代码的答案。 – 2015-04-06 07:26:20

+0

您的网址没有参数。你可以发布一些示例发布参数。 – Tarun 2015-12-11 11:06:05

1
import android.app.ProgressDialog; 
    import android.content.Context; 
    import android.os.AsyncTask; 

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

    public class MyAsyncTask extends AsyncTask<URL, Void, String> { 

     private HttpURLConnection urlConnection; 
     private Context mContext; 
     private ProgressDialog mDialog; 
     private TaskListener mListener; 

     public MyAsyncTask(Context context, TaskListener listener) { 
      this.mContext = context; 
      mDialog = new ProgressDialog(mContext); 
      this.mListener = listener; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      mDialog.setTitle(R.string.app_name); 
      mDialog.setMessage("Retrieving data..."); 
      mDialog.show(); 
     } 

     @Override 
     protected String doInBackground(URL... params) { 
      StringBuilder result = new StringBuilder(); 

      try { 
       URL url = params[0]; 
//   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080)); 
      urlConnection = (HttpURLConnection) url.openConnection(/*proxy*/); 
       urlConnection.setDoInput(true); 
       urlConnection.setConnectTimeout(20 * 1000); 
       urlConnection.setReadTimeout(20 * 1000); 

       if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 

        InputStream in = new BufferedInputStream(urlConnection.getInputStream()); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

        String line; 
        while ((line = reader.readLine()) != null) { 
         result.append(line); 
        } 
       } 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } finally { 
       urlConnection.disconnect(); 
      } 


      return result.toString(); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      mDialog.dismiss(); 
      mListener.onTaskComplete(s); 
     } 
    } 
相关问题