我的Java錯誤是:類型JSONParser中的方法makeHttpRequest(java.lang.String,java.lang.String,java.util.List)不適用於參數(java.lang.String,java.lang .String,java.util.List中) 我該如何解決makeHttpRequest錯誤?
我已經安裝了最新版本的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'也分享'jParser'類代碼與進口和'makeHttpRequest'方法模板,也表明' doInBackground'進口 –
我可以發佈我的jsonparser文件的真實代碼嗎?還有我的文件,我有這個問題?我不知道。 – Andreea
這是你可以看到我做錯了什麼的方式。我必須顯示2個文件,我有這個錯誤 – Andreea