用Okhttp3替换JSONParser

时间:2017-03-05 13:42:38

标签: android okhttp3

我已经考虑用更新的OKhttp3替换旧的JSONParser,但我发现的大多数示例都是基于表单构建,我需要简单替换Httppost。他们是否在OKhttp3库中直接替换HttpPost(paramString)?

package org.me.paths;

    import android.util.Log;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;

    // replace
    /* 
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.impl.client.DefaultHttpClient;
    */

    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.Response;
    import java.io.IOException;
    import org.json.JSONObject; 

    public class JSONParser
    {
      static InputStream is = null;
      static JSONObject jObj = null;
      static String json = "";

      public String getJSONFromUrl(String paramString)
      {
        Object localObject1;
        Object localObject2;
        try
        {
          DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
          //OkHttpClient client = new OkHttpClient();
          localObject1 = new HttpPost(paramString);

          localObject2 = localDefaultHttpClient.execute((HttpUriRequest)localObject1);
          HttpEntity localHttpEntity = ((HttpResponse)localObject2).getEntity();
          is = localHttpEntity.getContent();
        }
        catch (UnsupportedEncodingException localUnsupportedEncodingException)
        {
          localUnsupportedEncodingException.printStackTrace();
        }
        catch (ClientProtocolException localClientProtocolException)
        {
          localClientProtocolException.printStackTrace();
        }
        catch (IOException localIOException)
        {
          localIOException.printStackTrace();
        }
        try
        {
          BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

          localObject1 = new StringBuilder();
          localObject2 = null;
          while ((localObject2 = localBufferedReader.readLine()) != null) {
            ((StringBuilder)localObject1).append((String)localObject2 + "\n");
          }
          json = ((StringBuilder)localObject1).toString();
          is.close();
        }
        catch (Exception localException)
        {
          Log.e("Buffer Error", "Error converting result " + localException.toString());
        }
        return json;
      }
    }

我研究过并发现了以下内容:

  public String getJSONFromUrl(String paramString)
  {
    Object localObject1;
    Object localObject2;
  // Making HTTP request
        try {
            final OkHttpClient client = new OkHttpClient();
            Request request;
            // check for request method
            if (method.equals("POST")) {
                // request method is POST
                final MediaType JSON
                        = MediaType.parse("application/json; charset=utf-8");
                final RequestBody body = RequestBody.create(JSON, params.toString());
                //localObject1 =
                localObject1 = new Request.Builder()
                        .url(url)
                        .post(body)
                        .build();
            } else if (method.equals("GET")) {
                // request method is GET
                localObject1 = new Request.Builder()
                        .url(url)
                        .build();
            }
            final Response response = client.newCall(localObject2).execute();
            json = response.body().string();

        } catch (IOException e) {
            e.printStackTrace();
        }
        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e ){
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jObj;
    }

这是实现这个目标的方法吗?

0 个答案:

没有答案