Android 后参数给定误差
嗨,我已经创建了一个应用程序,点击一个按钮,它会向下一个活动发送一个字符串,在第二个活动中,我使用该值从数据库检索数据Android 后参数给定误差,android,mysql,android-intent,android-activity,Android,Mysql,Android Intent,Android Activity,嗨,我已经创建了一个应用程序,点击一个按钮,它会向下一个活动发送一个字符串,在第二个活动中,我使用该值从数据库检索数据 public class RecoProd extends Activity { EditText pd; TextView error; ArrayList<NameValuePair> postParameters; /** Called when the activity is first created. */ @O
public class RecoProd extends Activity {
EditText pd;
TextView error;
ArrayList<NameValuePair> postParameters;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
Class<?> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
.getContextClassLoader());
Class<?> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
.currentThread().getContextClassLoader());
Class<?> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
Thread.currentThread().getContextClassLoader());
Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);
Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
Method buildMethod = threadPolicyBuilderClass.getMethod("build");
Constructor<?> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();
Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);
obj = penaltyMethod.invoke(obj);
Object threadPolicyObject = buildMethod.invoke(obj);
setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);
} catch (Exception ex) {
String TAG = null;
Log.w(TAG, ex);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.recomain);
Intent myIntent = getIntent(); // getting the value from the previous activity
String lbl_name= myIntent.getStringExtra("lbl_name");//< get lbl_name from Intent
pd = (EditText) findViewById(R.id.name);
error = (TextView) findViewById(R.id.showresult);
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("product", lbl_name
));
// String valid = "1";
DownloadWebPageTask dw = new DownloadWebPageTask();
dw.execute("");
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = null;
for (String url : urls) {
try {
response = CustomHttpClient.executeHttpPost("http://192.168.1.7/abc/check2.php", postParameters);
String res = response.toString();
// res = res.trim();
res = res.replaceAll("\\s+", "");
// error.setText(res);
try{
res = "";
JSONArray jArray = new JSONArray(res);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("prod_id","id: "+json_data.getInt("prod_id")+
", prod_name: "+json_data.getString("prod_name")+
", prod_category: "+json_data.getString("prod_category")+
", prod_cost: "+json_data.getDouble("prod_cost")
);
//Get an output to the screen
res += "\n" + json_data.getInt("prod_id") + " -> "+ json_data.getString("prod_name");
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
try{
error.setText(res);
}
catch(Exception e){
Log.e("log_tag","Error in Display!" + e.toString());;
}
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
}
}
}
我正在做以下工作
Intent myIntent = getIntent(); // getting the value from the previous activity
myIntent.getStringExtra("lbl_name");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("product", lbl_name//passing the name to the database .getText().toString()));
访问数据库的代码
public class RecoProd extends Activity {
EditText pd;
TextView error;
ArrayList<NameValuePair> postParameters;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
Class<?> strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
.getContextClassLoader());
Class<?> threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
.currentThread().getContextClassLoader());
Class<?> threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
Thread.currentThread().getContextClassLoader());
Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);
Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
Method buildMethod = threadPolicyBuilderClass.getMethod("build");
Constructor<?> threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();
Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);
obj = penaltyMethod.invoke(obj);
Object threadPolicyObject = buildMethod.invoke(obj);
setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);
} catch (Exception ex) {
String TAG = null;
Log.w(TAG, ex);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.recomain);
Intent myIntent = getIntent(); // getting the value from the previous activity
String lbl_name= myIntent.getStringExtra("lbl_name");//< get lbl_name from Intent
pd = (EditText) findViewById(R.id.name);
error = (TextView) findViewById(R.id.showresult);
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("product", lbl_name
));
// String valid = "1";
DownloadWebPageTask dw = new DownloadWebPageTask();
dw.execute("");
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = null;
for (String url : urls) {
try {
response = CustomHttpClient.executeHttpPost("http://192.168.1.7/abc/check2.php", postParameters);
String res = response.toString();
// res = res.trim();
res = res.replaceAll("\\s+", "");
// error.setText(res);
try{
res = "";
JSONArray jArray = new JSONArray(res);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("prod_id","id: "+json_data.getInt("prod_id")+
", prod_name: "+json_data.getString("prod_name")+
", prod_category: "+json_data.getString("prod_category")+
", prod_cost: "+json_data.getDouble("prod_cost")
);
//Get an output to the screen
res += "\n" + json_data.getInt("prod_id") + " -> "+ json_data.getString("prod_name");
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
try{
error.setText(res);
}
catch(Exception e){
Log.e("log_tag","Error in Display!" + e.toString());;
}
}
catch (Exception e) {
Log.e("log_tag","Error in http connection!!" + e.toString());
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
}
}
}
公共类RecoProd扩展活动{
编辑文本pd;
文本视图错误;
ArrayList后参数;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
试一试{
Class strictModeClass=Class.forName(“android.os.StrictMode”,true,Thread.currentThread()
.getContextClassLoader());
Class threadPolicyClass=Class.forName(“android.os.StrictMode$ThreadPolicy”,true,线程
.currentThread().getContextClassLoader());
Class threadPolicyBuilderClass=Class.forName(“android.os.StrictMode$ThreadPolicy$Builder”,true,
Thread.currentThread().getContextClassLoader());
方法setThreadPolicyMethod=strictModeClass.getMethod(“setThreadPolicy”,threadPolicyClass);
方法detectAllMethod=threadPolicyBuilderClass.getMethod(“detectAll”);
方法penaltyMethod=threadPolicyBuilderClass.getMethod(“penaltyLog”);
方法buildMethod=threadPolicyBuilderClass.getMethod(“构建”);
构造函数threadPolicyBuilderConstructor=threadPolicyBuilderClass.getConstructor();
对象threadPolicyBuilderObject=threadPolicyBuilderConstructor.newInstance();
Object obj=detectAllMethod.invoke(threadPolicyBuilderObject);
obj=惩罚方法调用(obj);
Object threadPolicyObject=buildMethod.invoke(obj);
调用(strictModeClass,threadPolicyObject);
}捕获(例外情况除外){
字符串标记=null;
Log.w(标签,ex);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.recoain);
Intent myIntent=getIntent();//从上一个活动获取值
String lbl_name=myIntent.getStringExtra(“lbl_name”);// 对于(int i=0;i您需要从Intent中提取lbl\u name
值,如下所示:
Intent myIntent = getIntent(); // getting the value from the previous activity
String lbl_name= myIntent.getStringExtra("lbl_name");//< get lbl_name from Intent
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("product",lbl_name));
尝试在Intent
中传递字符串值,如下所示:
使用getString();
Intent myIntent=getIntent();//从上一个活动获取值
String lbl_name=myIntent.getString(“lbl_name”);
postParameters=新的ArrayList();
add(新的BasicNameValuePair(“product”,lbl_name//将名称传递给数据库。getText().toString());
显示将值放入Intent
的代码。您好,有人知道上述问题的解决方案吗,即使我面临同样的问题…@user1844638:此代码从prev Activity获取lbl_名称,并将其作为值添加到BasicNameValuePair中product@user1844638:plz在代码中共享调用数据库的代码,并解释有关 issue@user1844638:我看到了,您得到了服务器的响应吗?让我们看看
postParameters.add(new BasicNameValuePair("product",
Your_prev_Activity_Name.lbl_name));
Intent myIntent = new Intent(SinglePlaceActivity.this,RecoProd.class);
myIntent.putExtra("lbl_name", "lbl_name");
SinglePlaceActivity.this.startActivity(myIntent);
Intent myIntent = getIntent(); // getting the value from the previous activity
String lbl_name=myIntent.getString("lbl_name");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("product", lbl_name//passing the name to the database .getText().toString()));