Ich versuche, eine HTTP-Anfrage, mit einem Benutzernamen und Passwort params, die ich an den Benutzer mit einer Schnittstelle mit zwei EditText anfordern.Http Get Request mit URL Verbindung und Asyntask
Ich bekomme den folgenden Fehler ständig: "Bei der Ausführung von doInBackground() ist ein Fehler aufgetreten" Danke für die Hilfe.
Dies ist das Manifest:
<?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>
Hauptaktivität:
public class MainActivity extends Activity {
HttpURLConnection urlConnection = null;
InputStream is;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
final EditText password;
final EditText username;
Button login;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.txt_username);
password = (EditText) findViewById(R.id.txt_password);
login = (Button) findViewById(R.id.btn_loggin);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());
final String httpPath = "http://10.0.23.34/android/login.php?u=" + username.getText().toString() + "&p=" + password.getText().toString();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
new MyTask().execute();
}
});
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Und die Aufgabe Klasse:
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();
}
}
Am Ende habe ich die automatisch generierten die App codiert zu implementieren Indexierungs-API