我试图从远程数据库读取的数据库在我,对于我使用的是PHP的code连接到我的MySQL数据库和查询我的数据库:
该文件的名称:check.php:
< PHP
require_once(PHP / dbconnect.php);
$查询=选择从用户名;
$结果= mysql_query($查询)或死亡(无法验证用户,因为:。mysql_error());
而($行= mysql_fetch_array($结果)){
$输出[] = $行['名称'];
}
打印(json_en code($输出));
则mysql_close();
?/>
和Java(安卓)code连接到远程数据库:
进口java.io.BufferedReader中;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口的java.util.ArrayList;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.NameValuePair;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.entity.UrlEn codedFormEntity;
进口org.apache.http.client.methods.HttpPost;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.message.BasicNameValuePair;
进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.Toast;
进口java.net.URI中;
公共类ConnectToMyDb延伸活动{
InputStream的是;
私人HttpPost httppost;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
字符串结果=;
// HTTP POST
尝试{
HttpClient的HttpClient的=新DefaultHttpClient();
httppost =新HttpPost(http://www.Myurl.com/check.php);
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
Log.e(log_tag,连接成功);
Toast.makeText(getApplicationContext(),通,Toast.LENGTH_SHORT).show();
}赶上(例外五){
Log.e(log_tag,错误的HTTP连接+ e.toString());
Toast.makeText(getApplicationContext(),不合格,Toast.LENGTH_SHORT).show();
}
//转换响应串
尝试{
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
Toast.makeText(getApplicationContext(),通,Toast.LENGTH_SHORT).show();
}
is.close();
结果= sb.toString();
}赶上(例外五){
Log.e(log_tag,错误转换结果+ e.toString());
Toast.makeText(getApplicationContext(),不合格,Toast.LENGTH_SHORT).show();
}
//解析JSON数据
尝试{
// ArrayList的<邮件> RESULT1 =新的ArrayList<邮件>();
JSONArray jArray =新JSONArray(结果);
JSONObject的json_data =新的JSONObject的(结果);
的for(int i = 0; I< jArray.length();我++){
json_data = jArray.getJSONObject(ⅰ);
Log.i(log_tag,姓名:+ json_data.getString(姓名);
Toast.makeText(getApplicationContext(),通,Toast.LENGTH_SHORT).show();
}
}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
Toast.makeText(getApplicationContext(),不合格,Toast.LENGTH_SHORT).show();
}
}
我的问题它不是连接,但JSON的解析我得到一个异常:
错误解析数据org.json.JSONException:
值小于;!java.lang.String类型的DOCTYPE不能转换为JSONArray
解决方案
我会摆脱这行:
JSONArray jArray =新JSONArray(结果);
和这样做,而不是:
的JSONObject json_data =新的JSONObject的(结果);
JSONArray nameArray = json_data.names();
JSONArray的valarray = json.toJSONArray(nameArray);
我相信这是正确的,然而,请学习本教程,它会为你工作:
的 Android作为一个RESTful客户端
I am trying to read a database in my from a remote DB, for that i am using a php code to connect to my Mysql DB and to query my DB:
Name of the file: check.php:
<?php
require_once("php/dbconnect.php");
$query = "SELECT name FROM user ";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
while ( $row = mysql_fetch_array( $result ) ) {
$output[]=$row['name'];
}
print(json_encode($output));
mysql_close();
?/>
And the Java (Android) code to connect to the remote DB:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.net.URI;
public class ConnectToMyDb extends Activity {
InputStream is;
private HttpPost httppost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = "";
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.Myurl.com/check.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
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");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try{
//ArrayList<Messages> result1 = new ArrayList<Messages>();
JSONArray jArray = new JSONArray(result);
JSONObject json_data= new JSONObject(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
Log.i("log_tag"," name: "+json_data.getString("name");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
}
My problem its not the connection but the parsing of the json i get an exception:
Error parsing data org.json.JSONException:
Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
解决方案
I would get rid of this line:
JSONArray jArray = new JSONArray(result);
and do this instead:
JSONObject json_data = new JSONObject(result);
JSONArray nameArray = json_data.names();
JSONArray valArray = json.toJSONArray(nameArray);
I believe this is correct, nonetheless, please study this tutorial, it will work for you:
Android as a Restful Client