我想使用php从数据库中选择两个值,然后将其传递给android。

我想在AutoCompleteTextView中使用此值。

当我选择一个字段时,它将在AutoCompleteTextView上显示该字段并将ID存储在后台。

这是我的完整代码:-

       package com.example.sachin.addvisit;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends Activity {
    private WebView wv1;
    InputStream is=null;
    String result=null;
    String line=null;
    JSONObject jsonobject;
    private static String url_visitor = "http://10.0.2.2/portal/fetchwod.php";
    JSONParser jParser = new JSONParser();
    JSONArray ownerObj;
    ArrayList<String> arraylist;
    ArrayList<String> delivery_fetch = new ArrayList<String>();
    ArrayList<String> v_parties1 = new ArrayList<String>();
    ArrayList<String> order_fetch = new ArrayList<String>();
    String suid,uid,wt_wod_code1,wt_party1;
    TextView abcd,abc1;
    View view;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadJSON().execute();
        abcd =(TextView)findViewById(R.id.abc);
        abc1 =(TextView)findViewById(R.id.abc1);
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url ) {
            view.loadUrl(url);
            return true;
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... voids) {

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/autocomplete.php");
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("Pass 1", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 1", e.toString());
                Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_LONG).show();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                Log.e("Pass 2", "connection success ");
            } catch (Exception e) {
                Log.e("Fail 2", e.toString());
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void args) {
            try {
                JSONArray JA = new JSONArray(result);
                JSONObject json = null;
                final String[] str1 = new String[JA.length()];
                final String[] str2 = new String[JA.length()];

                for (int i = 0; i < JA.length(); i++) {
                    json = JA.getJSONObject(i);
                    str1[i] = json.getString("cl_pname");
                //    str2[i]= json.getString("cl_wodcode");
                }
                final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
                final List<String> list = new ArrayList<String>();

                for (int i = 0; i < str1.length; i++) {
                    list.add(str1[i]);
                }

                Collections.sort(list);
                final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.my_list_item, list);
                dataAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
                text.setThreshold(1);
                text.setAdapter(dataAdapter);
                text.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                        wv1=(WebView)findViewById(R.id.webView);
                        wv1.setWebViewClient(new MyBrowser());
                        wv1.loadUrl("http://10.0.2.2/portal/on_target.php?cod=RT13056");
                        wv1.getSettings().setLoadsImagesAutomatically(true);
                        wv1.getSettings().setJavaScriptEnabled(true);
                        wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
                        Toast.makeText(getApplicationContext(),(CharSequence)arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG).show();
                        wt_wod_code1 = text.getText().toString();
                        wt_party1 = text.getText().toString();
                          abc1.setText(text.getText());
                        //abc1.setText(text.getText());


                        Log.d("wt_wod_code",wt_party1);

                        new fill().execute();
                    }
                });
            } catch (Exception e) {
                Log.e("Fail 3", e.toString());
            }
        }
    }
    private class fill extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {

            // Create an array
            try {
                arraylist = new ArrayList<>();
                List<NameValuePair> params = new ArrayList<NameValuePair>(2);
                params.add(new BasicNameValuePair("cl_pname", uid));
                params.add(new BasicNameValuePair("cl_wodcode", suid));
                JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);

                int success1 = Integer.parseInt(json.getString("success4"));
                Log.d("success4", json.toString());

                if (success1 == 0) {
                    Snackbar.make(view, "Not Data Found", Snackbar.LENGTH_LONG).show();
                }
                if (success1 == 1) {
                    ownerObj = json.getJSONArray("visit");
                    for (int i = 0; i < ownerObj.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = ownerObj.getJSONObject(i);

                        delivery_fetch.add(jsonobject.getString("cl_wodcode"));
                        v_parties1.add(jsonobject.getString("cl_pname"));
                            Log.d("wt_wod_code",wt_wod_code1);
                            Log.d("wt_wod_code",wt_party1);
                        //}
                    }
                }
            } catch (Exception e) {
            }
            return null;
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }
}


这是我的API

 <?php

include("connection.php");

    $response = array();
    //$id = $_REQUEST["wt_wod_code"];
    //$uid=$id;


    $result = mysql_query("SELECT DISTINCT cl_wodcode,cl_pname FROM clients") or die(mysql_error());
    $count = mysql_num_rows($result);

    // check for empty result
    if ($count > 0) {
        // looping through all results
        // products node
        $response["visit"] = array();

        while ($row = mysql_fetch_array($result))
        {


            $Ordera = array();

            $visit["cl_wodcode"] = $row["cl_wodcode"];
            $visit["cl_pname"] = $row["cl_pname"];


            $response["success"]=1;


            // push single product into final response array
            array_push($response["visit"], $visit);
        }


            $response["success4"] = 1;
        // echoing JSON response
        echo json_encode($response);


    }

    else
    {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No picture found";

        // echo no users JSON
        echo json_encode($response);
    }


?>


请建议我如何传递两个值?

最佳答案

它显示什么样的错误?
可以 ?
而且,您要从JSON解析的两个值(cl_pname和cl_wodcode)是否都是字符串?

关于php - 如何将两个值API传递给Android?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40042053/

10-10 08:44