我尝试使用此网址 www.example.com/directory / 从服务器获取数据,但它无法正常工作......它想出了这个json没有向服务器发送任何动作......所以我想要的就是以这种方式制作网址 - www.example.com/directory/index.php?action=searchFood&money=50
这是我的JsonFetcher.java
public class JsonFetcher extends AsyncTask<Pair<String, String>, Integer, JSONObject> {
private JSONObject jsonObject;
public AsyncResponse delegate = null;
public interface AsyncResponse {
void processFinish(JSONObject output);
}
public JsonFetcher(AsyncResponse delegate) {
this.delegate = delegate;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
super.onPostExecute(jsonObject);
delegate.processFinish(jsonObject);
}
@Override
protected JSONObject doInBackground(final Pair<String, String>... params) {
int count = params.length;
URL url = null;
int responseCode = 0;
jsonObject = null;
try {
url = new URL("http://www.example.com/directory/");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
//conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
try {
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder();
Date date = new Date();
String md5 = date.toString();
//Log.e("json lock",lock);
//Log.e("json key",md5);
for (int i = 0; i < count; i++) {
builder.appendQueryParameter(String.valueOf(params[i].first),String.valueOf(params[i].second));
}
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// read the response
try {
responseCode = conn.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = null;
try {
in = new BufferedInputStream(conn.getInputStream());
} catch (IOException e) {
e.printStackTrace();
} finally {
//conn.disconnect();
}
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Log.e("json", total.toString());
try {
jsonObject = new JSONObject(total.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
}
是否有可能以这种方式写
url = new URL("http://www.example/directory/index.php?action= PairActionName & PairMoney" );
这里PairActionName和PairMoney将从MainActivity接收
答案 0 :(得分:0)
由于您使用AsyncTask执行服务器操作,因此您应创建一个编码查询字符串以将参数传递给url,并将其写入URLconnection对象。下面的例子将清楚我刚才所说的内容:
Uri.Builder builder = new Uri.Builder().appendQueryParameter("param1", value1)
.appendQueryParameter("param2", value2);
final String query = builder.build().getEncodedQuery();
URL url;
HttpURLConnection connection = null;
try{
url = new URL("http://www.example.com/directory/");
connection = (HttpURLConnection) url.openConnection();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
connection.connect();
}
在服务器端,您只需使用参数名称(param1)检索参数值(value1)。
此外,您可以使用.appendQueryParameter()
方法添加所需数量的参数。