我怎样才能解决方法makeHttpRequest错误?

时间:2015-11-16 20:11:15

标签: java android apache httprequest apache-httpclient-4.x

我的Java错误是: JSONParser类型中的方法makeHttpRequest(java.lang.String,java.lang.String,java.util.List)不适用于参数(java.lang.String, java.lang.String,java.util.List) enter image description here

我安装了最新版本 httpclient 和最新版本 httpcore 。    enter image description here

我的代码:

...........................................

     protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("NAME", restaurant_name));
        params.add(new BasicNameValuePair("tag", "check"));
        params.add(new BasicNameValuePair("client_name", client_name));
        JSONObject json = jParser.makeHttpRequest(url_info, "GET", params);
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);
                if (Integer.parseInt(res) == 1) {
                    success = 1;
                    info = json.getJSONArray(TAG_INFO);
                    for (int i = 0; i < info.length(); i++) {
                        JSONObject c = info.getJSONObject(i);
                        who = c.getString(TAG_CLIENT_NAME);
                        what = c.getString(TAG_WHAT);
                        total = c.getString(TAG_TOTAL);
                        note = c.getString(TAG_NOTE);
                        status = c.getString(TAG_STATUS);
                        getStatus = c.getString(TAG_GET_STATUS);
                    }
                } else if (Integer.parseInt(res) == 0) {
                    info = json.getJSONArray(TAG_INFO);
                    for (int i = 0; i < info.length(); i++) {
                        JSONObject c = info.getJSONObject(i);
                        getStatus = c.getString(TAG_GET_STATUS);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

...........................................

我的文件导入是:

 package *myapp*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import library.DatabaseHandler;
import library.JSONParser;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;

我的 JSONParser 文件是:

package library;
import android.util.Log;

import org.apache.http.NameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

public class JSONParser {

String charset = "UTF-8";
HttpURLConnection conn;
DataOutputStream wr;
StringBuilder result = new StringBuilder();
URL urlObj;
JSONObject jObj = null;
StringBuilder sbParams;
String paramsString;

public JSONObject makeHttpRequest(String url, String method,
                                  List<com.vrei.meniu.NameValuePair> params) {

    sbParams = new StringBuilder();
    int i = 0;
    for (int key : ((Object) params).keySet()) {
        try {
            if (i != 0){
                sbParams.append("&");
            }
            sbParams.append(key).append("=")
                    .append(URLEncoder.encode(charset));

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        i++;
    }

    if (method.equals("POST")) {
        // request method is POST
        try {
            urlObj = new URL(url);

            conn = (HttpURLConnection) urlObj.openConnection();

            conn.setDoOutput(true);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Accept-Charset", charset);

            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);

            conn.connect();

            paramsString = sbParams.toString();

            wr = new DataOutputStream(conn.getOutputStream());
            wr.writeBytes(paramsString);
            wr.flush();
            wr.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if(method.equals("GET")){
        // request method is GET

        if (sbParams.length() != 0) {
            url += "?" + sbParams.toString();
        }

        try {
            urlObj = new URL(url);

            conn = (HttpURLConnection) urlObj.openConnection();

            conn.setDoOutput(false);

            conn.setRequestMethod("GET");

            conn.setRequestProperty("Accept-Charset", charset);

            conn.setConnectTimeout(15000);

            conn.connect();

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

    }

    try {
        //Receive the response from the server
        InputStream in = new BufferedInputStream(conn.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

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

        Log.d("JSON Parser", "result: " + result.toString());

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

    conn.disconnect();

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

    // return JSON Object
    return jObj;
}

public JSONObject getJSONFromUrl(String registerURL, List<NameValuePair> params) {

    return null;
}

 }

enter image description here

2 个答案:

答案 0 :(得分:1)

我会在解析JSON

时使用它
try {
    DefaultHttpClient client = new DefaultHttpClient(new BasicHttpParams());
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 8000);
    HttpConnectionParams.setSoTimeout(httpParameters, 8000);
    client.setParams(httpParameters);

////////////////////////// Switch post to get if its a get//////////////////////////
    HttpPost post = new HttpPost("https://abc123.com/");

    JSONObject json = new JSONObject();
    json.put("name", "value");
    StringEntity se = new StringEntity(json.toString());
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    post.setEntity(se);
    HttpEntity entity = client.execute(post).getEntity();
    BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null)
        sb.append(line + "\n");
    String result = sb.toString();
    reader.close();

    if(result == null)
        break;  // <-----------------Just in case 

    JSONObject jsonObject = new JSONObject(result);

    ////////////////////////// Do parse the jsonObject here //////////////////////////

    } catch (Exception e) {
        e.printStackTrace();
    }

答案 1 :(得分:0)

makeHttpRequest方法需要List<org.apache.http.NameValuePair>类型,而您提供List<"my app".NameValuePair>类型,因此请将其设为List<org.apache.http.NameValuePair>或检查您的导入。< / p>