2011-10-16 61 views
0

我试图通过POST请求将数据从Android智能手机发送到REST Web服务。POST请求未到达服务器

有对客户端没有错误,但在服务器端是没有如果我发送的数据执行的代码:

客户:

public void connect2server(View v) throws IOException { 

    HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://192.168.1.108:8182"); 

try { 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "data")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

服务器端:

public class data_server extends ServerResource { 

public static void main(String[] args) throws Exception { 
    // Create the HTTP server and listen on port 8182 
    new Server(Protocol.HTTP, 8182, data_server.class).start(); 
} 

@Post 
public String toString1() throws IOException { 
    System.out.println(getRequestEntity().getText()); 
    return "ok"; 
} 

} 

这是如何造成的,我该如何解决这个问题?

回答

1

有没有错误,因为你正在捕捉错误,什么也不做。

尝试将以下内容添加到catch块中,您应该会看到LogCat输出中发生了什么。记得进口日志类

Log.w("com.name.pkg", e); 

其中“com.name.pkg”只是一个标签,所以帮助你过滤。通常是程序的名称。或者,通常使用的方法是向Toast.makeText(...)。show();发送一个Toast.txt.txt,然后发送Toast.makeText(...)。与e.getMessage()但我不喜欢这样做,所以我不推荐它。你最好把一个字符串传回给调用函数,并允许它做吐司。

+0

谢谢,ive添加了log命令,但logcat中没有显示错误... – FavoriteT

+0

你不需要设置你的url为'http://192.168.1.108:8182/toString' – Merlin