- Forum-Beiträge: 137
27.08.2013, 14:24:28 via Website
27.08.2013 14:24:28 via Website
ich bin kurz vorm verzweifeln.
Ich versuche meine Android App für einen einfachen login via PHP Script auf meine MySQL Datenbank zu verbinden.
Leider kommt es zu Verbindungsproblemen die Ich mir nicht erklären kann.
Hier der Code:
MainActivity:
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.InputStreamReader;
7import java.util.ArrayList;
8
9import org.apache.http.HttpEntity;
10import org.apache.http.HttpResponse;
11import org.apache.http.NameValuePair;
12import org.apache.http.client.HttpClient;
13import org.apache.http.client.entity.UrlEncodedFormEntity;
14import org.apache.http.client.methods.HttpPost;
15import org.apache.http.impl.client.DefaultHttpClient;
16import org.apache.http.message.BasicNameValuePair;
17import org.json.JSONObject;
18
19import android.os.Bundle;
20import android.app.Activity;
21import android.view.Menu;
22import android.view.View;
23import android.view.View.OnClickListener;
24import android.widget.Button;
25import android.widget.EditText;
26import android.widget.Toast;
27
28public class MainActivity extends Activity implements OnClickListener {
29
30 EditText etUser, etPass;
31 Button bLogin;
32
33 String username, password;
34
35 HttpClient httpclient;
36
37 HttpPost httppost;
38
39 ArrayList<NameValuePair> nameValuePairs;
40
41 HttpResponse response;
42
43 HttpEntity entity;
44
45 @Override
46 protected void onCreate(Bundle savedInstanceState) {
47 super.onCreate(savedInstanceState);
48 setContentView(R.layout.activity_main);
49
50 initialise();
51 }
52
53 private void initialise() {
54 // TODO Auto-generated method stub
55
56 etUser = (EditText) findViewById(R.id.etUser);
57 etPass = (EditText) findViewById(R.id.etPass);
58 bLogin = (Button) findViewById(R.id.button1);
59
60 bLogin.setOnClickListener(this);
61
62 }
63
64 @Override
65 public boolean onCreateOptionsMenu(Menu menu) {
66 // Inflate the menu; this adds items to the action bar if it is present.
67 getMenuInflater().inflate(R.menu.main, menu);
68 return true;
69 }
70
71 @Override
72 public void onClick(View arg0) {
73 // TODO Auto-generated method stub
74 try {
75 httpclient = new DefaultHttpClient();
76
77 httppost = new HttpPost("http://192.168.X.X/project/login.php");
78
79 username = etUser.getText().toString();
80 password = etPass.getText().toString();
81
82 } catch (Exception e) {
83
84 e.printStackTrace();
85 Toast.makeText(getBaseContext(), "Problem mit der Deklaration",
86 Toast.LENGTH_SHORT).show();
87 }
88
89 try {
90
91 nameValuePairs = new ArrayList<NameValuePair>();
92
93 nameValuePairs.add(new BasicNameValuePair("user", username));
94 nameValuePairs.add(new BasicNameValuePair("pass", password));
95
96 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
97
98 try {
99 response = httpclient.execute(httppost);
100 } catch (Exception e) {
101 e.printStackTrace();
102 Toast.makeText(getBaseContext(),
103 "Verbindung zur Zieladresse nicht möglich",
104 Toast.LENGTH_SHORT).show();
105 }
106
107 if (response.getStatusLine().getStatusCode() == 200) {
108
109 entity = response.getEntity();
110
111 if (entity != null) {
112 InputStream instream = entity.getContent();
113
114 JSONObject jsonResponse = new JSONObject(
115 convertStreamToString(instream));
116
117 String retUser = jsonResponse.getString("user");
118 String retPass = jsonResponse.getString("pass");
119
120 if (username.equals(retUser) && password.equals(retPass)) {
121
122 Toast.makeText(getBaseContext(), "login success!",
123 Toast.LENGTH_SHORT).show();
124
125 } else {
126
127 Toast.makeText(getBaseContext(), "Invalid login",
128 Toast.LENGTH_SHORT).show();
129 }
130 }
131 }
132 } catch (Exception e) {
133
134 e.printStackTrace();
135
136 Toast.makeText(getBaseContext(), "Connection Error",
137 Toast.LENGTH_SHORT).show();
138 }
139
140 }
141
142 private static String convertStreamToString(InputStream is) {
143 /*
144 * To convert the InputStream to String we use the
145 * BufferedReader.readLine() method. We iterate until the BufferedReader
146 * return null which means there's no more data to read. Each line will
147 * appended to a StringBuilder and returned as String.
148 */
149
150 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
151 StringBuilder sb = new StringBuilder();
152
153 String line = null;
154 try {
155 while ((line = reader.readLine()) != null) {
156 sb.append(line + "\n");
157 }
158 } catch (IOException e) {
159 e.printStackTrace();
160 } finally {
161 try {
162 is.close();
163 } catch (IOException e) {
164 e.printStackTrace();
165 }
166 }
167 return sb.toString();
168
169 }
170
171}
Manifest:
2<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.mysqlconnect10"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="17" />
10 <uses-permission android:name="android.permission.INTERNET"/>
11 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
12
13 <application
14 android:allowBackup="true"
15 android:icon="@drawable/ic_launcher"
16 android:label="@string/app_name"
17 android:theme="@style/AppTheme" >
18 <activity
19 android:name="com.example.mysqlconnect10.MainActivity"
20 android:label="@string/app_name" >
21 <intent-filter>
22 <action android:name="android.intent.action.MAIN" />
23
24 <category android:name="android.intent.category.LAUNCHER" />
25 </intent-filter>
26 </activity>
27 </application>
28
29</manifest>
PHP Script:
2
3
4$username = $_POST=['username'];
5$password = $_POST=['password'];
6
7$db_host="localhost";
8$user = "root";
9$pswd = "";
10$db = "mobiledb";
11
12
13$conn = mysql_connect($db_host, $user, $pswd);
14mysql_select_db($db, $conn);
15
16$query = 'SELECT * FROM androidlogin WHERE user = "$username" AND pass = "$password"';
17
18$result = mysql_query($query) or die ('unable');
19
20$num = mysql_num_rows($result);
21
22if($num == 1)
23{
24 while($list=mysql_fetch_assoc($result)){
25
26 $output = $list;
27
28 echo json_encode($output);
29
30
31 }
32 mysql_close();
33
34 echo "Login succ";
35
36}
37else
38{
39
40echo "Login failed";
41}
42
43
44
45?>
Das ganze läuft auf einen Error: Die Exception "Verbindung zur Zieladresse nicht möglich" greift.
Das PHP Script liefert im Test ohne die App die richtigen Daten zurück. Somit sollte es an der App liegen.
Weiß jemand warum?
Danke schon einmal
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.