我是android编程新手,我想做一个小程序,从一个特定的API-URL下载字符串。(我对总体编程并不陌生)。
现在,我被困在下面的代码中,只需从url下载我的字符串:
String urlToDownloadToken = baseUrl + "?action=login&username=xxx&password=xxx";
Object taskResult = new DownloadString().execute(urlToDownloadToken);下载类的实现如下。在callbnack函数中,理论上应该显示数据,但它总是会让人兴奋(我找到的代码来自于这里: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();
}}
发布于 2015-10-26 15:58:26
您可以参考以下示例代码:
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的截图

希望这能有所帮助!
发布于 2015-10-26 15:29:05
HttpClient已经成为已弃用多年了,已经从Android6中删除了。你应该使用OkHttp,这是一个新的标准。这要容易得多:)。
https://stackoverflow.com/questions/33349392
复制相似问题