从Android向php传递参数
在我的Android中:从Android向php传递参数,php,android,mysql,parameters,Php,Android,Mysql,Parameters,在我的Android中: String cat_id = getIntent().getExtras().getString("category_id"); ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("cat_id", cat_id)); try {
String cat_id = getIntent().getExtras().getString("category_id");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cat_id", cat_id));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.---.com/items.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
String cat_id=getIntent().getExtras().getString(“category_id”);
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“cat_id”,cat_id));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://www.---.com/items.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
在我的PHP中:
// editing out DB connections which have been verified to work
<?php
$sql=mysql_query("SELECT * FROM items WHERE cat_id = '".$_REQUEST['cat_id']."'");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
//编辑出已验证有效的DB连接
这条线不应该吗
$sql=mysql_query("SELECT * FROM items WHERE cat_id = '".$_REQUEST['cat_id']."'");
是
i、 e.$\请求$\发送邮件
我建议使用Firefox的Firebug插件,并跟踪post发送到服务器的确切形式。我知道是什么,没有回答是谁的错
这个问题是在Java传给PHP之前开始的。这是一个传递到相关Java活动中的包。我犯了一个简单的错误,使用了一个不正确的原语,这反过来导致通过bundle传入null
空值沿着路径继续,并到达PHP部分。所以上面显示的代码实际上是正确的 CustomList.java:->
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final String[] imageId;
Context ct;
public CustomList(Activity context,
String[] web, String[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
ct=context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
TextView txtval=(TextView)rowView.findViewById(R.id.val);
//Typeface tf=Typeface.createFromAsset(ct.getAssets(), "saral.ttf");
//Typeface tf=Typeface.createFromAsset(ct.getAssets(), "shrutib.ttf");
//txtTitle.setTypeface(tf);
//txtTitle.setText(web[position]);
//txtval.setTypeface(tf);
txtval.setText(imageId[position]);
//ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
//imageView.setImageResource(imageId[position]);
return rowView;
}
}
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Typeface;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类CustomList扩展了ArrayAdapter{
私人最终活动背景;
私有最终字符串[]web;
私有最终字符串[]imageId;
背景ct;
公共自定义列表(活动上下文,
字符串[]web,字符串[]imageId){
super(上下文、R.layout.list_single、web);
this.context=上下文;
this.web=web;
this.imageId=imageId;
ct=上下文;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.list_single,null,true);
TextView txtTitle=(TextView)rowView.findViewById(R.id.txt);
TextView txtval=(TextView)rowView.findViewById(R.id.val);
//Typeface tf=Typeface.createFromAsset(ct.getAssets(),“saral.ttf”);
//Typeface tf=Typeface.createFromAsset(ct.getAssets(),“shrutib.ttf”);
//txtTitle.setTypeface(tf);
//setxt(web[位置]);
//txtval.setTypeface(tf);
txtval.setText(imageId[位置]);
//ImageView ImageView=(ImageView)rowView.findViewById(R.id.img);
//setImageResource(imageId[位置]);
返回行视图;
}
}
MainActivity.java:->
package learn2crack.customlistview;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
ListView list;
String result = null;
InputStream is = null;
StringBuilder sb = null;
String[] web=null;
String[] imageId = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("link of php page");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// paring data
//JSONObject jArray;
JSONArray jArray;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
web = new String[jArray.length()];
imageId=new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
web[i] = json_data.getString("category_id");
imageId[i] = json_data.getString("category_name");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent newint=new Intent(getApplicationContext(), subcatcls.class);
newint.putExtra("cat_id",web[+ position]);
startActivity(newint);
}
});
}
}
package learn2crack.customlistview;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.net.ParseException;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入android.app.Activity;
导入android.content.Intent;
公共类MainActivity扩展了活动{
列表视图列表;
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
字符串[]web=null;
字符串[]imageId=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“php页面链接”);
//setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//配对数据
//JSONObject jArray;
杰索纳雷·贾雷;
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
web=新字符串[jArray.length()];
imageId=新字符串[jArray.length()];
for(int i=0;ipackage learn2crack.customlistview;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
ListView list;
String result = null;
InputStream is = null;
StringBuilder sb = null;
String[] web=null;
String[] imageId = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("link of php page");
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// paring data
//JSONObject jArray;
JSONArray jArray;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
web = new String[jArray.length()];
imageId=new String[jArray.length()];
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
web[i] = json_data.getString("category_id");
imageId[i] = json_data.getString("category_name");
}
} catch (JSONException e1) {
Toast.makeText(getBaseContext(), "No City Found", Toast.LENGTH_LONG).show();
} catch (ParseException e1) {
e1.printStackTrace();
}
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent newint=new Intent(getApplicationContext(), subcatcls.class);
newint.putExtra("cat_id",web[+ position]);
startActivity(newint);
}
});
}
}