2010-07-06 83 views
0

我想将数据从客户端(安卓)发送到服务器,是我的格式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"}]}, 

我试图多次测试,但没有用,这可怎么办呢?

+2

单从严格苛刻点,你永远不应该将数据推送到服务器URL的一部分。因此,名称,GET操作应该只检索数据,不要更改数据。这是为POST操作保留的 – 2010-07-06 04:28:08

+0

您是否正确地转义了URL? – bhups 2010-07-06 04:35:44

回答

0

没有你的代码,很难评估你是否正确地完成了这个过程,但是根据你给我们的,你没有正确编码数据。在追加到网址之前,您需要对数据进行网址编码。在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的倡导者。我在这里使用它只是为了生成一些示例代码。

如果这样不能解决您的问题,请发布您的代码,以便我们可以看到您正在尝试执行的操作。使用

+0

感谢兄弟,这是我看,工作完美,非常感谢 – Apache 2010-07-06 09:13:34

0

库: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. 
       } 
      }); 
} 
};