【问题标题】:How to send data to servlet from android application in android studio? [duplicate]如何从android studio中的android应用程序向servlet发送数据? [复制]
【发布时间】:2016-06-14 22:15:24
【问题描述】:

我想将数据从我的 android 应用程序发送到 servlet。 我试图在我的应用程序中使用它:

    private boolean postData(String s) {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(s); // s is my url

    try {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("id", "1003"));
        nameValuePairs.add(new BasicNameValuePair("name", "Anil"));
        nameValuePairs.add(new BasicNameValuePair("loc", "Kalyan"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        return false;
    }
    return true;
}

我是这个方法但无法导入这些文件:

    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;

如何在 android studio 中导入这些文件?或者有没有其他方法可以做到这一点?提前感谢

【问题讨论】:

    标签: java android servlets


    【解决方案1】:

    Android 6.0 版本删除了对 Apache HTTP 客户端的支持。如果您的应用正在使用此客户端并针对 Android 2.3(API 级别 9)或更高版本,请改用 HttpURLConnection 类

     URL url = new URL(path);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setRequestMethod("POST");
     conn.setConnectTimeout(10000);
     conn.setReadTimeout(2000);
     conn.setDoOutput(true);
     byte[] bypes = params.toString().getBytes();
     conn.getOutputStream().write(bypes);
     InputStream inStream = conn.getInputStream();
    

    【讨论】:

    • Thanx @lynn518,我从来没有用过这个,你能不能给我发一些通过android向servlet发送数据的例子以及如何接收它?
    • 我很高兴这样做,但很抱歉我不知道如何发送给您
    • 能否给我发邮件? @ roverma1094@gmail.com 或者链接一些示例 plz
    • 我已经发了一些链接到你的邮箱,希望对你有帮助
    • Thanx 会试试看的:)
    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 2014-11-17
    • 2013-04-23
    • 2020-10-07
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多