如何使用android通过id从数据库中检索数据

时间:2017-05-08 03:58:57

标签: android mysql listview

我正在使用select query和condition = tid从数据库中检索数据,因为我想知道如何在select查询中传递服务器上的tid。请给我任何解决方案如何在服务器上发送tid并从数据库中检索特定tid的数据。感谢。

一些代码

 new GetHttpResponse(this).execute();

    listCollege .setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            TextView tasknameOBJ=(TextView)view.findViewById(R.id.taskname);
            TextView taskidOBJ = (TextView)view.findViewById(R.id.tid);
             tid = taskidOBJ.getText().toString();
            taskname = tasknameOBJ.getText().toString();
            Toast.makeText(ListTask.this,"You selected Id is "+tid,Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(ListTask.this,GetSubTask.class);
            startActivity(intent);

            SharedPreferences sharedPreferences = ListTask.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);

            SharedPreferences.Editor editor = sharedPreferences.edit();


            editor.putString(Config.TID_SHARED_PREF, tid);
            editor.putString(Config.TASKNAME_SHARED_PREF, taskname);
            //Saving values to editor
            editor.commit();
        }
    });
}
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
    private Context context;
    String result;
    List<Task> Task_;
    public GetHttpResponse(Context context)
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0)
    {
        HttpService httpService = new HttpService("http://192.168.0.104/GroupBuilder/GetListTask.php");
        try
        {
            httpService.ExecutePostRequest();

            if(httpService.getResponseCode() == 200)
            {
                result = httpService.getResponse();

                System.out.println("Result       . . . "+result);

                Log.d("Result", result);
                if(result != null)
                {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(result);

                        JSONObject object;
                        JSONArray array;
                        Task_ = new ArrayList<Task>();
                        for(int i=0; i<jsonArray.length(); i++)
                        {
                            task_ = new Task();
                            object = jsonArray.getJSONObject(i);

                            task_.taskname = object.getString("taskname");
                            task_.tid = object.getString("tid");

                            System.out.println("Taask Tid  "+task_.tid);
                            Task_.add(task_);
                        }
                    }
                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            else
            {
                Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        proCollageList.setVisibility(View.GONE);
        listCollege.setVisibility(View.VISIBLE);
        if(Task_ != null)
        {
            ListTaskAdapter adapter = new ListTaskAdapter(Task_, context);
            listCollege.setAdapter(adapter);
        }
    }
}//

public class HttpService
{

    private ArrayList <NameValuePair> params;
    private ArrayList <NameValuePair> headers;

    private String url;
    private int responseCode;
    private String message;
    private String response;

    public String getResponse()
    {
        return response;
    }

    public String getErrorMessage()
    {
        return message;
    }

    public int getResponseCode()
    {
        return responseCode;
    }

    public HttpService(String url)
    {
        this.url = url;
        params = new ArrayList<NameValuePair>();
        headers = new ArrayList<NameValuePair>();
    }

    public void AddParam(String name, String value)
    {
        params.add(new BasicNameValuePair(name, value));
    }

    public void AddHeader(String name, String value)
    {
        headers.add(new BasicNameValuePair(name, value));
    }


    public void ExecutePostRequest() throws Exception
    {
        HttpPost request = new HttpPost(url);
        for(NameValuePair h : headers)
        {
            request.addHeader(h.getName(), h.getValue());
        }

        if(!params.isEmpty())
        {
            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        }

        executeRequest(request, url);
    }

    private void executeRequest(HttpUriRequest request, String url)
    {
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 10000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 10000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        HttpClient client = new DefaultHttpClient(httpParameters);
        HttpResponse httpResponse;
        try
        {
            httpResponse = client.execute(request);
            responseCode = httpResponse.getStatusLine().getStatusCode();
            message = httpResponse.getStatusLine().getReasonPhrase();

            HttpEntity entity = httpResponse.getEntity();
            if (entity != null)
            {
                InputStream instream = entity.getContent();
                response = convertStreamToString(instream);
                instream.close();
            }
        }
        catch (ClientProtocolException e)
        {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        }
        catch (IOException e)
        {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        }
    }

    private String convertStreamToString(InputStream is)
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try
        {
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

非常感谢你。

0 个答案:

没有答案