我想將數據從客戶端(安卓)發送到服務器,是我的格式Android的HTTP POST與參數
http://101.34.45.45/rawData?data={"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]},
我試圖多次測試,但沒有用,這可怎麼辦呢?
我想將數據從客戶端(安卓)發送到服務器,是我的格式Android的HTTP POST與參數
http://101.34.45.45/rawData?data={"userId":"guest1","timestamp":"2010-07-01 08:58:23","wifi":[{"ssid":"guest","rssi":"40"},{"ssid":"guest1","rssi":"80"}]},
我試圖多次測試,但沒有用,這可怎麼辦呢?
沒有你的代碼,很難評估你是否正確地完成了這個過程,但是根據你給我們的,你沒有正確編碼數據。在追加到網址之前,您需要對數據進行網址編碼。在java中使用URLEncoder
類(see the javadoc for that class here)。
要與您的代碼做到這一點,就不會使用UTF-8看起來像
String url = "http://101.34.45.45/rawData?data=";
String params = URLEncoder.encode("{\"userId\":\"guest1\",\"timestamp\":\"2010-07-01 08:58:23\",\"wifi\":[{\"ssid\":\"guest\",\"rssi\":\"40\"},{\"ssid\":\"guest1\",\"rssi\":\"80\"}]}", "UTF-8");
url = url+params;
//do the HTTP operation
*注意,爲了完整起見,W3C和javadoc的倡導者。我在這裏使用它只是爲了生成一些示例代碼。
如果這樣不能解決您的問題,請發佈您的代碼,以便我們可以看到您正在嘗試執行的操作。使用
感謝兄弟,這是我看,工作完美,非常感謝 – Apache 2010-07-06 09:13:34
庫:http://loopj.com/android-async-http/
private OnClickListener login = new OnClickListener() {
public void onClick(View view) {
AsyncHttpClient myClient = new AsyncHttpClient();
myClient.get(URL, null);
myClient.setCookieStore(myCookieStore);
myClient.setCookieStore(myCookieStore);
String username = "";
String password = "";
RequestParams params1 = new RequestParams();
params1.put("username", username);
params1.put("password", password);
pd = ProgressDialog.show(this, "", "Signing In...");
myClient.post(URL, params1,
new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println("response" + response);
pd.dismiss();
if (response.contains("<!--Authorized-->")) {
}
else {
pd.dismiss();
Context mContext = SigninActivity.this;
notMatchDialog = new Dialog(mContext);
notMatchDialog.setContentView(R.layout.loginfaileddialoglayout);
notMatchDialog.setTitle("Login failed");
dismissDialogButton = (Button) notMatchDialog.findViewById(R.id.dismissDialogButton);
dismissDialogButton.setOnClickListener(dismissDialog);
notMatchDialog.show();
}
}
@Override
public void onFailure(Throwable e, String response) {
// TODO Need to figure out different failures and try to help the user.
}
});
}
};
單從嚴格苛刻點,你永遠不應該將數據推送到服務器URL的一部分。因此,名稱,GET操作應該只檢索數據,不要更改數據。這是爲POST操作保留的 – 2010-07-06 04:28:08
您是否正確地轉義了URL? – bhups 2010-07-06 04:35:44