首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用HttpClient下载总是返回空响应

使用HttpClient下载总是返回空响应
EN

Stack Overflow用户
提问于 2015-10-26 15:22:35
回答 2查看 342关注 0票数 0

我是android编程新手,我想做一个小程序,从一个特定的API-URL下载字符串。(我对总体编程并不陌生)。

现在,我被困在下面的代码中,只需从url下载我的字符串:

代码语言:javascript
复制
String urlToDownloadToken = baseUrl + "?action=login&username=xxx&password=xxx";
Object taskResult = new DownloadString().execute(urlToDownloadToken);

下载类的实现如下。在callbnack函数中,理论上应该显示数据,但它总是会让人兴奋(我找到的代码来自于这里:https://stackoverflow.com/a/14418213):

编辑:应用推荐使用OkHttp后的完整源代码

代码语言:javascript
复制
public class MusicScroll extends AppCompatActivity {

String baseUrl = "http://ppcinj.com";
String token = "";

AlertDialog.Builder dlgAlert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_scroll);

    //Set MessageBox properties...
    dlgAlert = new AlertDialog.Builder(this);
    dlgAlert.setCancelable(true);
    dlgAlert.setTitle("Message from Application");
    dlgAlert.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    try {
        String urlToDownloadToken = baseUrl + "?action=login&username=xxx&password=xxx";
        token = downloadString(urlToDownloadToken);
    } catch (Exception e) {
        dlgAlert.setMessage("Error downloading data: " + e.getMessage());
        dlgAlert.create().show();
    }

    dlgAlert.setMessage(token);
    dlgAlert.create().show();
}

OkHttpClient client = new OkHttpClient();

String downloadString(String url) throws IOException {
    Request request = new Request.Builder()
            .url(url)
            .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
}

}有什么方法可以像C#的WebClient那样简单地下载吗?

亲切的问候:)

编辑2:让它使用以下代码:)

代码语言:javascript
复制
public class MusicScroll extends AppCompatActivity {

String baseUrl = "http://ppcinj.tk:5656";
String token = "";

AlertDialog.Builder dlgAlert;

Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message message) {
        if (message.what == 1) {
            Toast.makeText(getApplicationContext(), token, Toast.LENGTH_LONG).show();
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_scroll);

    //Set MessageBox properties...
    dlgAlert = new AlertDialog.Builder(this);
    dlgAlert.setCancelable(true);
    dlgAlert.setTitle("Message from Application");
    dlgAlert.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    try {
        String urlToDownloadToken = baseUrl + "?action=login&username=michael&password=qwerty123";

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(urlToDownloadToken)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.e("BNK", e.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.i("BNK", response.toString());
                token = response.body().string();
                Message msg = mHandler.obtainMessage(1);
                msg.sendToTarget();
            }
        });
    } catch (Exception e) {
        dlgAlert.setMessage("Error downloading data: " + e.getMessage());
        dlgAlert.create().show();
    }
}

public void showToken()
{
    Toast.makeText(getApplicationContext(), token, Toast.LENGTH_LONG).show();
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-26 15:58:26

您可以参考以下示例代码:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);            
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://ppcinj.com?action=login&username=michael&password=qwerty123")
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {                    
                Log.e("BNK", e.toString());
            }

            @Override
            public void onResponse(Response response) throws IOException {                    
                Log.i("BNK", response.toString());
            }
        });
    }
}

这是logcat的截图

希望这能有所帮助!

票数 0
EN

Stack Overflow用户

发布于 2015-10-26 15:29:05

HttpClient已经成为已弃用多年了,已经从Android6中删除了。你应该使用OkHttp,这是一个新的标准。这要容易得多:)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33349392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档