我一直在尝试将两个变量(从 Android 中的 Spinner 和 EditText 检索)从我的 Android 应用程序发送到 php 页面。这些变量将在 php 中使用 $_REQUEST 检索,以便它们可以在 MySQL 查询中使用。但我仍然无法检测到我的代码的问题:
public class Taxi_searchActivity extends Activity {
String keyword =null;
String para =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//StrictMode.enableDefaults();
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.para_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Button btn_search=(Button) findViewById(R.id.btn_search);
EditText txt_keyword = (EditText) findViewById(R.id.txt_keyword);
keyword = txt_keyword.getText().toString();
para=spinner.getSelectedItem().toString();
// Search button click event
btn_search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching SearchActivity
Intent i = new Intent(getApplicationContext(), SearchActivity.class);
i.putExtra("keyval", keyword);
i.putExtra("paraval",para);
startActivity(i);
}
});
我正在从第一个活动taxisearchactivity 中检索变量,并在其启动期间(通过互联网)将它们发送到第二个活动。
这是通过 json 对象 (httppost) 发送数据的第二个活动。第一个活动的变量存储在数组列表中,如下所示:
public class SearchActivity extends Activity {
/** Called when the activity is first created. */
String keywrd=null;
String parameter=null;
String result=null;
InputStream isr=null;
TextView resultView;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.enableDefaults();
TextView resultView=(TextView)findViewById(R.id.res);
Intent i = getIntent();
keywrd = i.getStringExtra("keyval");
parameter=i.getStringExtra("paraval");
nameValuePairs.add(new BasicNameValuePair("keywrd", keywrd));
nameValuePairs.add(new BasicNameValuePair("parameter", parameter));
getData();
}
public void getData()
{
String result="";
try
{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://192.168.1.45/abc/ser.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
isr=entity.getContent();
}
catch(Exception e)
{
Log.e("log_tag","Error in http connection "+e.toString());
resultView.setText("Couldn't connect to database");
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag","Error in converting result"+e.toString());
resultView.setText("Couldn't convert");
}
try
{
String s="";
JSONArray jArray=new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json=jArray.getJSONObject(i);
s=s+
"Taxi id: "+json.getString("taxi_id")+"\n"+
"Taxi No: "+json.getString("taxi_no")+"\n\n";
}
resultView.setText(s);
}
catch(Exception e)
{
Log.e("log_tag","Error parsing data"+e.toString());
resultView.setText("Couldn't parse");
}
这是我的 php 代码:
<?php
require "conn.php";
$ser=$_REQUEST['keywrd']; //VALUE FROM ANDROID EDITTEXT
$ddl=$_REQUEST['parameter'];// VALUE FROM ANDROID SPINNER
$query="";
if($ddl=="Taxi_No")
{
$query="select taxi_id,taxi_no from taxi_info where taxi_no like '%$ser%'";
}
if($ddl=="Driver_Name")
{
$query="select taxi_id,taxi_no from taxi_info where driver_name like '%$ser%'";
}
if($ddl=="Source")
{
$query="select taxi_id,taxi_no from taxi_info where `from` like '%$ser%'";
}
if($ddl=="Destination")
{
$query="select taxi_id,taxi_no from taxi_info where 'to' like '%$ser%'";
}
$result=mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
?>
应用程序在单击搜索按钮(第一个活动)时停止工作。这是我的日志:
04-23 23:16:21.024: E/log_tag(335): Error parsing datajava.lang.NullPointerException
04-23 23:16:21.043: E/AndroidRuntime(335): FATAL EXCEPTION: main
04-23 23:16:21.043: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to start activity ComponentInfo{search.taxi.info/search.taxi.info.SearchActivity}: java.lang.NullPointerException
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.os.Looper.loop(Looper.java:123)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 23:16:21.043: E/AndroidRuntime(335): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 23:16:21.043: E/AndroidRuntime(335): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 23:16:21.043: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 23:16:21.043: E/AndroidRuntime(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 23:16:21.043: E/AndroidRuntime(335): at dalvik.system.NativeStart.main(Native Method)
04-23 23:16:21.043: E/AndroidRuntime(335): Caused by: java.lang.NullPointerException
04-23 23:16:21.043: E/AndroidRuntime(335): at search.taxi.info.SearchActivity.getData(SearchActivity.java:110)
04-23 23:16:21.043: E/AndroidRuntime(335): at search.taxi.info.SearchActivity.onCreate(SearchActivity.java:52)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 23:16:21.043: E/AndroidRuntime(335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-23 23:16:21.043: E/AndroidRuntime(335): ... 11 more
谢谢