Android 带有urlConnection和Asyntask的Http Get请求
我试图发出一个http请求,使用一个用户名和密码参数,通过一个带有两个EditText的界面向用户请求 我不断收到以下错误:“执行doInBackground()时发生错误”。感谢您的帮助 这是舱单:Android 带有urlConnection和Asyntask的Http Get请求,android,android-asynctask,httpurlconnection,http-get,Android,Android Asynctask,Httpurlconnection,Http Get,我试图发出一个http请求,使用一个用户名和密码参数,通过一个带有两个EditText的界面向用户请求 我不断收到以下错误:“执行doInBackground()时发生错误”。感谢您的帮助 这是舱单: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.leonelb
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leonelbaidal.moodlenots">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
以及任务类:
private class MyTask extends AsyncTask<String, Void, Void>{
String textResult;
@Override
protected Void doInBackground(String... httpPath){
try {
URL url = new URL(httpPath[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String stringBuffer;
String stringText = "";
while ((stringBuffer = in.readLine()) != null) {
stringText += stringBuffer;
}
in.close();
textResult = stringText;
} catch (MalformedURLException e) {
e.printStackTrace();
textResult = e.toString();
} catch (IOException e){
e.printStackTrace();
textResult = e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result){
Toast.makeText(getApplicationContext(), textResult, Toast.LENGTH_SHORT).show();
}
}
私有类MyTask扩展了AsyncTask{
字符串文本结果;
@凌驾
受保护的Void doInBackground(字符串…httpPath){
试一试{
URL URL=新URL(httpPath[0]);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setDoOutput(true);
BufferedReader in=新的BufferedReader(新的InputStreamReader(urlConnection.getInputStream());
字符串缓冲区;
字符串stringText=“”;
而((stringBuffer=in.readLine())!=null){
stringText+=stringBuffer;
}
in.close();
textResult=stringText;
}捕获(格式错误){
e、 printStackTrace();
textResult=e.toString();
}捕获(IOE异常){
e、 printStackTrace();
textResult=e.toString();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
Toast.makeText(getApplicationContext(),textResult,Toast.LENGTH_SHORT).show();
}
}
最后,我对自动生成的代码进行了编码,以实现应用程序索引API。您缺少url输入参数
new MyTask().execute(httpPath);
new MyTask().execute(httpPath);