1

Android アプリから 2 つの変数 (Android の Spinner と EditText から取得) を php ページに送信しようとしています。これらの変数は、$_REQUEST を使用して php で取得されるため、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);

        }
    });

1 番目のアクティビティ taxsearchactivity から変数を取得し、その開始時に (インターネット経由で) 2 番目のアクティビティに送信しています。

これは、json オブジェクト (httppost) を介してデータを送信する 2 番目のアクティビティです。最初のアクティビティの変数は、以下に示すように配列リストに格納されます。

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));



?>

アプリケーションは、検索ボタンをクリックすると動作を停止します (最初のアクティビティ)。これは私のlogcatです:

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

ありがとう

4

1 に答える 1