【问题标题】:How can i solve method makeHttpRequest error?如何解决方法 makeHttpRequest 错误?
【发布时间】:2015-11-16 20:11:14
【问题描述】:

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

我已经安装了最新版本 httpclient 和最新版本 httpcore

我的代码:

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

     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;
}

 }

【问题讨论】:

  • 检查NameValuePair 在两个类中的导入是否相同,并与导入和makeHttpRequest 方法模板共享jParser 类代码,并显示doInBackground 导入
  • 我可以发布我的 jsonparser 文件的真实代码吗?还有我的文件哪里有这个问题?我不知道。
  • 这是你可以看到我做错了什么的方式。我必须显示 2 个有此错误的文件
  • 是的,进口。不是整个文件只是从两个类和 makeHttpRequest 方法模板中导入

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


【解决方案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();
    }

【讨论】:

    【解决方案2】:

    makeHttpRequest 方法需要 List&lt;org.apache.http.NameValuePair&gt; 类型,而您为其提供 List&lt;"my app".NameValuePair&gt; 类型,因此将其创建为 List&lt;org.apache.http.NameValuePair&gt; 或检查您的导入。

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多