簡體   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