我无法从数据库中获取数据。。我的数据库是lcoalhost/phpmyadmin我想从我的表中选择一个特定的数据。。
这是我的php代码(select.php):
include_once("connect.php");
$sqlString = "select * from loans where sssnumber = $sssnumber";
$rs = mysql_query($sqlString);
if($rs){
while($objRs = mysql_fetch_assoc($rs)){
$output[] = $objRs;
}
echo json_encode($output);
}
mysql_close();
这是我的android代码(home.java):
public class Home extends Activity {
Button btnn;
TextView txt1,txt2,txt3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
new task().execute();
btnn = (Button)findViewById(R.id.button1);
btnn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Home.this, Lista.class);
startActivity(i);
}
});
}
class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(Home.this);
InputStream is = null ;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Fetching data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
task.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
String url_select = "http://XXX.XXX.X.X/XXX/select.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line=br.readLine())!=null)
{
sb.append(line+"'n");
}
is.close();
result=sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result "+e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
// ambil data dari Json database
try {
JSONArray Jarray = new JSONArray(result);
for(int i=0;i<Jarray.length();i++)
{
JSONObject Jasonobject = null;
txt1 = (TextView)findViewById(R.id.txt1);
txt2 = (TextView)findViewById(R.id.txt2);
txt3 = (TextView)findViewById(R.id.txt3);
Jasonobject = Jarray.getJSONObject(i);
//get an output on the screen
String no = Jasonobject.getString("sssnumber");
String name = Jasonobject.getString("firstname");
String birthday = Jasonobject.getString("middlename");
txt1.setText(no);
txt2.setText(name);
txt3.setText(birthday);
}
this.progressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
}
我的JSONParser:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser(){
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// 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();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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;
}
}
帮助。
错误登录安卓:
02-04 22:02:21.089: E/linker(6348): load_library(linker.cpp:759): library "libmaliinstr.so" not found
02-04 22:02:21.093: E/(6348): appName=com.example.s_s_s, acAppName=com.android.cts.openglperf
02-04 22:02:21.093: E/(6348): 0
02-04 22:02:21.093: E/(6348): appName=com.example.s_s_s, acAppName=com.android.browser
02-04 22:02:21.093: E/(6348): 0
02-04 22:02:21.324: E/log_tag(6348): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
请检查您的报表
$sqlString = "select * from loans where sssnumber = $sssnumber";
1) 从中获取变量$sssnumber
的值。
2) 如果$sssnumber
是一个变量,那么上面写的语句是不正确的。
正确的陈述将是
$sqlString = "select * from loans where sssnumber = '".$sssnumber."';
试试这个并检查
根据您的错误日志,您没有收到有效的json数据,这意味着这个问题与php有关。
我不知道你是否发布了完整的php脚本,但它有一些问题:
$sssnumber
似乎未定义。您可能需要$_POST['sssnumber']
- 您的查询易受sql注入攻击。您应该清除/转义您的变量(或者使用准备好的语句…)
- 您使用的是一个不推荐使用的mysqlapi。您应该切换到PDO或mysqli,并使用准备好的语句来避免sql注入
以上任何一种情况都可能导致php警告,从而使json无效。
<?php
include_once("connect.php");
$sqlString = "select * from loans";
$rs = mysql_query($sqlString);
if($rs){
while($objRs = mysql_fetch_assoc($rs)){
$output[] = $objRs;
}
echo json_encode($output);
}
mysql_close();
?>
这段php代码是正确的,它选择了所有细节,但只选择了最新插入的数据。。我有点想得到的是与所说的身份证号码具体的数据
示例表字段:
身份证号码
名字
中间名
编辑:
我的id表名称是sssnum
第二张表为fname
第三个表是mname
如果我得到的id编号是123,那么,唯一显示的结果是id编号123的名字和中间名