本文介绍了Android的HTTP客户端的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能找到这个问题。我有一个应用程序,在服务器的通信完全正常。不幸的是,我移动到Windows 8的CP时,莫名其妙地失去了我的Eclipse工作区。我仍然有.apk文件,并使用Dex2jar和JD贵,我能够挽救很多code的。我有这一切回到工作状态,但这。我试图将网址发送到服务器,并取回一个字符串的反应。这里的code:

I'm hoping someone can find this problem. I had an app that was fully working in server communications. Unfortunately, I somehow lost my Eclipse workspace when moving to the Windows 8 CP. I still had the .apk, and using Dex2jar and jd-gui, I was able to salvage a lot of code. I've got it all back into working condition, but this. I'm attempting to send a URL to a server, and get back a string response. Here's the code:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class login extends Activity{

<code>

public void pushLogin(View paramView){

  try{
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(loginFinal);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    errorTextView.setText(loginFinal);

    //code gets here
    String response = client.execute(request, responseHandler);
    //does not get here
    errorTextView.setText(response);
  }

我的TextView总是包含字符串loginFinal,我不能让它显示的响应。试图获取字符串响应之后来行;要检查这个问题,我提出的 errorTextView.setText(loginFinal)。它没有在这一点上无论是运行。我撕我的头发,我敢肯定,这是简单的东西。我已经得到了互联网许可,我甚至发现我原来的code的应用在这个网站,因为我的贴吧问一个单独的问题这一部分。这code是,据我所知道的,相同的。我能想到的,改变的唯一的事情是我提出我的构建目标从升级Froyo到蜂巢,我决定,我想专注于平板电脑。

My TextView always contains the string loginFinal, I cannot get it to display the response. To check this, I moved the errorTextView.setText(loginFinal); to the line after attempting to get the String response. It didn't run at that point either. I'm tearing my hair out, and I'm sure it's something simple. I've got the internet permission, I even found my original code for this portion of the app on this site as I posted it asking a separate question. This code is, as far as I can tell, identical. The only thing I can think of that changed is I moved my build target from Froyo to Honeycomb, as I decided I want to focus on tablets.

最好的部分是,LogCat中也绝对没有什么,当我preSS按钮,触发pushLogin。它似乎并没有在所有被触发 client.execute(请求,ResponseHandler所)

The best part is that LogCat does absolutely nothing when I press the button, triggering pushLogin. It doesn't seem to be triggering the client.execute(request, responseHandler) at all.

推荐答案

您可能正在调用pushLogin()在UI线程,需要注意的是线程的政策一直以来API等级11(蜂窝),在短,不改变允许网络操作(包括HttpClient的和HttpURLConnection类)在UI线程得到执行,否则,你得到NetworkOnMainThreadException。正确的策略是调用后台线程pushLogin()(AsycnTask作为一个很好的例子)。

You are probably call pushLogin() on UI thread, Note that the thread policy has been changed since API Level 11 (HONEYCOMB), which in short, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread, otherwise you get NetworkOnMainThreadException. The correct strategy is to call pushLogin() on background thread (AsycnTask as a good example).

希望这有助于。

这篇关于Android的HTTP客户端的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:32
查看更多