我想问一些关于android编程的问题..so,我从一个名为list_movie的类中得到了一个值,这是代码>我只张贴了一半
public void adapter_listview() {
ListAdapter adapter = new SimpleAdapter(this, dftr,R.layout.list_item,new String[] { "title", "id"}, new int[] {R.id.tangk, R.id.kodeangk});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String kode_ang = ((TextView) view.findViewById(R.id.kodeangk)).getText().toString();
Intent in = new Intent(getApplicationContext(), studio.class);
in.putExtra("kode_intent", kode_ang);
startActivity(in);
}
});
}
因此,我有一个包含id的值“ kode_intent”(此id是从表中的id_movie字段获取的)。接下来,在名为studio的类中使用它。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.studio);
Bundle b = getIntent().getExtras();
kode = b.getString("kode_intent");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("id_movie", kode.getText().toString())); >> this line "kode" is getting an error
所以我的问题是,如何将
kode = b.getString("kode_intent")
的值传递给postParameters.add(new BasicNameValuePair("id_movie", kode.getText().toString()));
这是我的PHP代码:
$db_host = "127.0.0.1";
$db_uid = "root";
$db_pass = "";
$db_name = "platinum";
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql = "Select studio_name, date from cinema where id_movie = '". $_POST["id_movie"]."'";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();
由于我是android编程的新手,所以任何帮助对我来说都是如此。
谢谢
我试图将
postParameters.add(new BasicNameValuePair("id_film", kode.getText().toString()));
更改为此一个postParameters.add(new BasicNameValuePair("id_movie", kode));
看起来不错..但是现在我又遇到了一个错误。代码如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.studio);
Bundle b = getIntent().getExtras();
kode = b.getString("kode_intent");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("id_movie", kode));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/cinemainfo/movie_time.php");
HttpResponse response = httpclient.execute(httppost,postParameters);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "success!");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
我在此行
HttpResponse response = httpclient.execute(httppost,postParameters);
>>上出现错误,执行错误消息:“类型为HttpClient的方法execute(HttpUriRequest,HttpContext)不适用于参数(HttpPost,数组列表)”
我做错什么了吗???
最佳答案
postParameters.add(new BasicNameValuePair("id_movie", kode.getText().toString()));
“未为类型String定义方法getText()”
postParameters.add(new BasicNameValuePair("id_movie", kode);
修复后,您将遇到另一个错误
NetworkOnMainThreadException
您有2个解决方案:
StrictMode选中此answer
AsyncTask
建议使用第二个选项。