我无法从我的数据库中获取数据。我的数据库是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();这是我的安卓代码(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;
}
}帮助。
android错误日志:
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发布于 2015-02-04 22:12:01
请检查您的对账单
$sqlString = "select * from loans where sssnumber = $sssnumber";1)从哪里获取变量$sssnumber的值。
2)如果$sssnumber是一个变量,那么上面的语句是错误的。
正确的陈述将是
$sqlString = "select * from loans where sssnumber = '".$sssnumber."';试试这个,然后检查
发布于 2015-02-04 22:23:14
根据你的错误日志,你没有收到有效的json数据,所以这意味着这个问题与php有关。
我不知道你是否发布了完整的php脚本,但它有一些问题:
$sssnumber似乎未定义。你应该清理/转义你的变量(或者使用一个已经准备好的statement...).以上任何一项都可能导致php警告,从而使您的json无效。
发布于 2015-02-04 22:32:13
<?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代码是正确的,它选择了所有的细节,但它只选择了最近插入的data..what,我想要的是获得带有上述ID号的数据
示例表字段:
ID号
名字
中间名
编辑:
我的id表名是sssnum
第二个表是fname
第三个表是mname
如果我得到的id号是123,那么唯一显示的结果就是id号123的名字和中间名。
https://stackoverflow.com/questions/28323005
复制相似问题