
joshua1996
Stammgast
Hallo,
zur Zeit arbeite ich an meiner (ersten) Android-App. Nun Versuche ich, mich mit der App einzuloggen (die App ist Bestandteil eines Schulprojektes) Die Daten beziehe ich über PHP aus einer MySQL-Datenbank. Die Daten sollen dann über JSON ausgegeben werden und von der Android App gelesen werden.
Als Vorlage hab ich das Tutorial verwendet: Android JSON Parsing from URL - Example
Nun habe ich Versucht die Daten über GET einfach an die URL zu senden, was nur bedingt funktioniert hat (die App hat sich aufgehangen/eingefroren, kein FC)
Dann hab ich ein wenig dazu gegooglet und hab mich über den Async-Task informiert und eingebaut. Nun bekomme ich einen Forceclose wenn ich auf den Login-Button klicke.
Der Logcat sagt mir folgendes:
{ "user": [ { "id": "11", "bool": "true" } ] }
Bei fehlgeschlagenem Login:
{ "user": [ { "id": "0", "bool": "false" } ] }
Ich vermute mal das dass Problem bei meinem asyncronem Task liegt, weiß aber nicht wo genau... Bin für jede Hilfe dankbar
zur Zeit arbeite ich an meiner (ersten) Android-App. Nun Versuche ich, mich mit der App einzuloggen (die App ist Bestandteil eines Schulprojektes) Die Daten beziehe ich über PHP aus einer MySQL-Datenbank. Die Daten sollen dann über JSON ausgegeben werden und von der Android App gelesen werden.
Als Vorlage hab ich das Tutorial verwendet: Android JSON Parsing from URL - Example
Nun habe ich Versucht die Daten über GET einfach an die URL zu senden, was nur bedingt funktioniert hat (die App hat sich aufgehangen/eingefroren, kein FC)
Dann hab ich ein wenig dazu gegooglet und hab mich über den Async-Task informiert und eingebaut. Nun bekomme ich einen Forceclose wenn ich auf den Login-Button klicke.
Der Logcat sagt mir folgendes:
Mein Code der Login-Activity:03-21 17:12:50.615: I/ActivityManager(1036): START u0 {cmp=de.swg/.MainActivity (has extras)} from pid 3561
03-21 17:12:50.618: W/ContextImpl(1268): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1244 android.content.ContextWrapper.sendBroadcast:365 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.PauseResumeTrigger.handleFrameworkEvent:53 com.motorola.motocare.internal.frameworkevents.FwEventMonitor$FrameworkListener.processFrameworkEvent:114
03-21 17:12:50.673: W/ContextImpl(1268): Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast:1244 android.content.ContextWrapper.sendBroadcast:365 com.motorola.motocare.util.TriggerHelper$TriggerBuilder.send:76 com.motorola.motocare.internal.frameworkevents.PauseResumeTrigger.handleFrameworkEvent:53 com.motorola.motocare.internal.frameworkevents.FwEventMonitor$FrameworkListener.processFrameworkEvent:114
03-21 17:12:50.714: W/ResourceType(3561): No package identifier when getting value for resource number 0x00000000
03-21 17:12:50.715: D/AndroidRuntime(3561): Shutting down VM
03-21 17:12:50.715: W/dalvikvm(3561): threadid=1: thread exiting with uncaught exception (group=0x41644d40)
03-21 17:12:50.720: E/AndroidRuntime(3561): FATAL EXCEPTION: main
03-21 17:12:50.720: E/AndroidRuntime(3561): Process: de.swg, PID: 3561
03-21 17:12:50.720: E/AndroidRuntime(3561): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.swg/de.swg.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread.access$800(ActivityThread.java:139)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.os.Handler.dispatchMessage(Handler.java:102)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.os.Looper.loop(Looper.java:136)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread.main(ActivityThread.java:5102)
03-21 17:12:50.720: E/AndroidRuntime(3561): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 17:12:50.720: E/AndroidRuntime(3561): at java.lang.reflect.Method.invoke(Method.java:515)
03-21 17:12:50.720: E/AndroidRuntime(3561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-21 17:12:50.720: E/AndroidRuntime(3561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-21 17:12:50.720: E/AndroidRuntime(3561): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:126)
03-21 17:12:50.720: E/AndroidRuntime(3561): at dalvik.system.NativeStart.main(Native Method)
03-21 17:12:50.720: E/AndroidRuntime(3561): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.content.res.Resources.getText(Resources.java:244)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.content.res.XResources.getText(XResources.java:508)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.widget.TextView.setText(TextView.java:3894)
03-21 17:12:50.720: E/AndroidRuntime(3561): at de.swg.MainActivity.onCreate(MainActivity.java:34)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.Activity.performCreate(Activity.java:5248)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
03-21 17:12:50.720: E/AndroidRuntime(3561): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
03-21 17:12:50.720: E/AndroidRuntime(3561): ... 12 more
03-21 17:12:50.736: W/ActivityManager(1036): Force finishing activity de.swg/.MainActivity
03-21 17:12:50.737: W/ActivityManager(1036): Force finishing activity de.swg/.LoginActivity
Die JSON-Parser Klasse:package de.swg;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends Activity {
private EditText EtEMAIL;
private EditText EtPASSWORD;
private TextView Error;
private ProgressDialog pDialog;
String success = null;
String id = null;
//URL to get JSON Array
private static String url1 = "http://www.bkconnect.bplaced.net/JSON/json_login.php?email=";
String url = "";
//JSON Node Names
private static final String TAG_USER = "user";
private static final String TAG_ID = "id";
private static final String TAG_EMAIL = "email";
private static final String TAG_PASSWORD = "password";
private static final String TAG_BOOL = "bool";
JSONArray user = null;
//Shardeprefs-Strings für das einspeichern von E-Mail
private final String PREFS_EMAIL ="...";
private final String PREFS_SAVEEMAIL = "...";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
EtEMAIL = (EditText)findViewById(R.id.eTEmail);
EtPASSWORD = (EditText)findViewById(R.id.eTPassword);
Error = (TextView)findViewById(R.id.textView1);
EtEMAIL.setText(getSharedPreferences(PREFS_EMAIL, MODE_PRIVATE).getString(PREFS_SAVEEMAIL, null));
}
public void ClickAnmelden (View v) {
EtEMAIL = (EditText)findViewById(R.id.eTEmail);
EtPASSWORD = (EditText)findViewById(R.id.eTPassword);
Error = (TextView)findViewById(R.id.textView1);
if (EtEMAIL.getText().toString().length() == 0 || EtPASSWORD.getText().toString().length() == 0) {
Error.setVisibility(View.VISIBLE);
}
else {
getSharedPreferences(PREFS_EMAIL, MODE_PRIVATE).edit().putString(PREFS_SAVEEMAIL, EtEMAIL.getText().toString()).commit();
url = url1 + EtEMAIL.getText().toString() + "&password" + EtPASSWORD.getText().toString();
new Login().execute();
Intent newIntent = new Intent(this, MainActivity.class);
newIntent.putExtra("success", success);
newIntent.putExtra("id", id);
startActivity(newIntent);
}
}
/**
* Background Async Task to Login
* */
class Login extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Loggin in ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Saving product
* */
protected String doInBackground(String... args) {
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting JSON Array
user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);
// Storing JSON item in a Variable
String id = c.getString(TAG_ID);
String bool = c.getString(TAG_BOOL);
} catch (JSONException e) {
e.printStackTrace();
}
return id;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product updated
pDialog.dismiss();
}
}
}
Das PHP-Script gibt mir bei erfolgreichem Login folgendes zurück:package de.swg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
{ "user": [ { "id": "11", "bool": "true" } ] }
Bei fehlgeschlagenem Login:
{ "user": [ { "id": "0", "bool": "false" } ] }
Ich vermute mal das dass Problem bei meinem asyncronem Task liegt, weiß aber nicht wo genau... Bin für jede Hilfe dankbar