עבור לתוכן

בעיה בהתחברות לmysql מאנדרואיד

Featured Replies

פורסם

אני בונה אפליקציה פשוטה יחסית שתדע לקרוא/לכתוב ממסד הנתונים:

הבעיה שכל פעם שאני מריץ את התוכנית אני מקבל את השגיאה הבאה:

Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject

הקוד של האפליקציה:



package a.a.a;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class TestMeActivity extends Activity {


JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Button but = (Button) findViewById(R.id.button1);
but.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView tx = (TextView)findViewById(R.id.textView1);
//tx.setText(testDB());
JSONObject obj = testDB("[url]http://10.0.2.2/test.php"[/url]);
}
});
}


public JSONObject testDB(String url){
String json = null;
JSONObject jObj = null;
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//httpPost.setEntity(new UrlEncodedFormEntity(params));

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();
Log.e("JSON", json);
} 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;

}
}

אני משתמש בשרת apache ודף test.php שדרכו תתנהל ההתחברות עם המסד הנתונים

קוד של הtest.php


<?php
mysql_connect("localhost","root","1234");
mysql_select_db("active");
$sql=mysql_query("select * from test");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
//print ("[{\"Error\":\"No Recrods\"}]");
mysql_close();
?>

הקוד של דף הphp עובד בלי שום בעיה, כשאני מדאבג את התוכנית אני מקבל שהמשתנה result(באפליקציה) מכיל קוד שגיאה 404

מה יכולה להיות הבעיה?

תודה

  • 2 חודשים מאוחר יותר...
פורסם

תתחיל מזה שיש לך

jObj = new JSONObject(json);

על זה הוא בוכה לך נראה לי...

דבר שני עזוב אותך מהגישה הזאת.. אתה רוצה לכתוב לSQL ולקרוא? קח פתרון נוח וקל יותר.

במקרה יצא לי בשבוע שעבר לכתוב בעצמי אפליקציה לאנדרואיד שהיה לי צורך לגישה מהירה לכתוב ולקרוא מSQL LITE.

אולי יעזור לך הקישור הבא http://android10.org/index.php/articlesdatastorage/252-content-providers-using-and-creating-them

לפני זה הייתי מציע אולי שתקרא קצת על sqlLite באתר הראשי

http://developer.android.com/guide/topics/data/data-storage.html#db

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים