繁体   English   中英

HTTP 请求无响应

[英]No response from HTTP request

我编写了一个将数据发布到我的数据库的 android 应用程序。 该应用程序应访问将数据发布到数据库的网络服务。 网络服务工作正常。 我用我的浏览器测试过,他已经在服务器上了。 现在我希望我的应用程序执行网络服务。 但这不起作用。 我的调试器也不起作用,所以我无法调试。 这是我用于访问网络服务的代码。 有任何想法吗??

public class PostBlog extends AsyncTask<String, Void, String> {
String BlogURL;

public PostBlog(String insertBlogURL) {
    BlogURL = insertBlogURL;
}

@Override
protected String doInBackground(String... params) {
    postBlogData(BlogURL);
    return null;
}

public void postBlogData(String url) {

    String result = "";
    //the year data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("year", "1980"));

    //http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();

        result = sb.toString();
    } catch (Exception e) {

        //(TextView)rootView.findViewById(R.id.question)
        Log.e("log_tag", "Error converting result " + e.toString());
    }


}

}

该类是从我的主要活动中调用的

new PostBlog(insertBlogURL).execute("");

是否有另一种更简单的方法在服务器上执行我的“.jsp?asdd=sdsd”文件?

谢谢你的想法。

而不是做:

new PostBlog(insertBlogURL).execute("");

通过执行params[0]更改构造函数并从doInBackground方法中检索 url

然后像这样启动下载

PostBlog blogPoster = new PostBlog();
try {
    blogPoster.execute(insertBlogURL);
} catch (InterruptedException e) {} catch (ExecutionException e) {}

我应该说这是我自己项目中修改后的代码片段,因此它可能无法完全按照您的预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM