2015-11-16 113 views
0

我的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我該如何解決makeHttpRequest錯誤?

我已經安裝了最新版本的HttpClient 最後一個版本的HttpCoreenter 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

+0

時,我會用這個代替檢查進口兩種類是同爲'NameValuePair'也分享'jParser'類代碼與進口和'makeHttpRequest'方法模板,也表明' doInBackground'進口 –

+0

我可以發佈我的jsonparser文件的真實代碼嗎?還有我的文件,我有這個問題?我不知道。 – Andreea

+0

這是你可以看到我做錯了什麼的方式。我必須顯示2個文件,我有這個錯誤 – Andreea

回答

0

makeHttpRequest方法需要List<org.apache.http.NameValuePair>類型,當你給它一個List<"my app".NameValuePair>類型,所以將其創建爲List<org.apache.http.NameValuePair>或檢查你的進口。

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