gpt4 book ai didi

php - 选择php中的代码到android

转载 作者:行者123 更新时间:2023-11-29 23:05:07 25 4
gpt4 key购买 nike

我无法从我的数据库获取数据..我的数据库是 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;

}

}

帮助。

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

最佳答案

请检查您的声明

$sqlString = "select * from loans where sssnumber = $sssnumber";

1) 从哪里获取变量 $sssnumber 的值。

2) 如果$sssnumber是一个变量,那么上面的语句是不正确的。

正确的说法是

 $sqlString = "select * from loans where sssnumber = '".$sssnumber."';

尝试一下并检查

关于php - 选择php中的代码到android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28323005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com