我编写了一个程序,想要从数据库中读取一些数据,它显示我在数据库中创建的星期六表中存在的所有数据,但我希望use子句显示一些数据。实际上在这个程序中,我想要显示每个人登录的数据,他的名字在我称之为whoo的字符串中(查看android代码); 如何更改它只读取与登录人员相关的数据。 在我的表中是一行我称之为user_name,在android代码中我有一个名为whoo的字符串,我希望用这个子句显示数据: 用户名=' $ username' 请帮我... 我用PHP和json编写这个程序 这是我的代码:
PHP:
<?php
$con = mysql_connect("...","...","....");
mysql_select_db("...",$con);
$response=array();
$result=mysql_query("select * from saturday");
if(mysql_num_rows($result)>0){
$response["saturday"]=array();
while($row=mysql_fetch_array($result)){
$temp=array();
$temp["username"]=$row["user_name"];
$temp["userside"]=$row["user_sideup"];
$temp["userchair"]=$row["user_chair"];
$temp["userjump"]=$row["user_jump"];
$temp["userstep"]=$row["user_step"];
$temp["usercalf"]=$row["user_calf"];
array_push($response["saturday"],$temp);
}
$response["t"]=1;
echo json_encode($response);
}else{
$response["t"]=0;
$response["message"]="not fonud";
echo json_encode($response);
}
?>
这是我的android:
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ResultPage extends ListActivity {
private ProgressDialog pd;
JSONParser jparser = new JSONParser();
ArrayList<HashMap<String, String>> P;
JSONArray s = null;
private static String url = "http://akosamanus.xzn.ir/getsaturday.php";
TextView who,which;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_page);
who = (TextView)findViewById(R.id.who);
which = (TextView)findViewById(R.id.which);
Bundle extras = getIntent().getExtras();
String whoo = extras.getString("uname");
String whiche = extras.getString("uday");
who.setText(whoo);
which.setText(whiche);
P = new ArrayList<HashMap<String, String>>();
new result().execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String zname = ((TextView) v.findViewById(R.id.tvsetname)).getText().toString();
String zside = ((TextView) v.findViewById(R.id.tvsetside)).getText().toString();
String zstep = ((TextView) v.findViewById(R.id.tvsetstep)).getText().toString();
String zcalf = ((TextView) v.findViewById(R.id.tvsetcalf)).getText().toString();
String zjump = ((TextView) v.findViewById(R.id.tvsetjump)).getText().toString();
String zchair = ((TextView) v.findViewById(R.id.tvsetchair)).getText().toString();
Intent intent = new Intent(ResultPage.this,CResultPage.class);
intent.putExtra("zside",zside);
intent.putExtra("zstep",zstep);
intent.putExtra("zcalf",zcalf);
intent.putExtra("zjump",zjump);
intent.putExtra("zchair",zchair);
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode==1){
if(data.hasExtra("Result")){
}
}
}
class result extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(ResultPage.this);
pd.setMessage("loading...");
pd.show();
}
@Override
protected String doInBackground(String... argman) {
// ContentValues params = new ContentValues();
List<NameValuePair> params = new ArrayList<NameValuePair>(); // Building Parameters
// getting JSON string from URL
JSONObject json = jparser.makeHttpRequest(url, "GET", params);
try {
int t = json.getInt("t");
if (t==1){
s=json.getJSONArray("saturday");
for (int i=0;i<s.length();i++){
JSONObject c=s.getJSONObject(i);
String username=c.getString("username");
String userside=c.getString("userside");
String userchair=c.getString("userchair");
String userjump=c.getString("userjump");
String userstep=c.getString("userstep");
String usercalf=c.getString("usercalf");
String today=which.getText().toString();
HashMap<String,String> map=new HashMap<String,String>();
map.put("username",username);
map.put("userside",userside);
map.put("userchair",userchair);
map.put("userjump",userjump);
map.put("userstep",userstep);
map.put("usercalf", usercalf);
map.put("Saturday", today);
P.add(map);
}
}else {
Toast.makeText(ResultPage.this, "ooooooooooops", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter=new SimpleAdapter(ResultPage.this,P,R.layout.item_list2,
new String[]{"username","userside","userchair","userjump","userstep","usercalf","Saturday"},
new int[]{R.id.tvsetname,R.id.tvsetside,R.id.tvsetchair,R.id.tvsetjump,R.id.tvsetstep
,R.id.tvsetcalf,R.id.tvsetday});
setListAdapter(adapter);
}
});
}
}
}
08-14 13:14:11.433 4305-4305/com.example.eagle.start1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.eagle.start1, PID: 4305
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
at com.example.eagle.start1.JSONParser.makeHttpRequest(JSONParser.java:118)
at com.example.eagle.start1.ResultPage$result$1.run(ResultPage.java:135)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
对于从数据库获取特定值,我们必须使用GET或POST方法 所以我在这里修改了你的PHP代码(使用GET方法)。
<?php
$con = mysql_connect("...","...","....");
mysql_select_db("...",$con);
if (isset($_GET["username"])) {
$username = $_GET['username'];
$response=array();
$result=mysql_query("select * from saturday where user_name= $username");
if(mysql_num_rows($result)>0){
$response["saturday"]=array();
while($row=mysql_fetch_array($result)){
$temp=array();
$temp["username"]=$row["user_name"];
$temp["userside"]=$row["user_sideup"];
$temp["userchair"]=$row["user_chair"];
$temp["userjump"]=$row["user_jump"];
$temp["userstep"]=$row["user_step"];
$temp["usercalf"]=$row["user_calf"];
array_push($response["saturday"],$temp);
}
$response["t"]=1;
echo json_encode($response);
}else{
$response["t"]=0;
$response["message"]="not fonud";
echo json_encode($response);
}
}
?>
在Android端,您必须在NameValuePair中传递user_name,如下所示
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",who.getText()));
final JSONObject json = jParser.makeHttpRequest(url, "GET", params);
如果您有任何疑问,只需观看this或在下面发表评论:) 快乐的编码!