gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:00:46 26 4
gpt4 key购买 nike

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

现在我被以下代码困住了,只是为了从网址下载我的字符串:

String urlToDownloadToken = baseUrl + "?action=login&username=xxx&password=xxx";
Object taskResult = new DownloadString().execute(urlToDownloadToken);

下载类的实现如下。在 callbnack 函数中,我有一个 toast,理论上应该显示数据,但它总是做出一个空的 toast(我找到的代码来自这里:https://stackoverflow.com/a/14418213):

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

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:让它与以下代码一起使用:)

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();
}

}

最佳答案

HttpClient 已 deprecated已有多年,并已从 Android 6 中删除。您应该使用 OkHttp相反,它是新标准。而且它要容易得多:)。

关于java - 使用 HttpClient 下载总是返回空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com