如何在数据库中存储登录数据....?

时间:2016-06-24 07:11:18

标签: android android-webservice

我是Android的新手,我正在开发自己的应用。在这里我想连接数据库。我找到了一个教程,我按照完整的代码进行了操作,但它没有工作。在那段代码上,我得到了红色的彩色文字。

       try
    {
        HttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost("http://192.168.1.218/testdemo/login.php");

        List<NameValuePair> list=new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("name", valuse[0]));
        list.add(new BasicNameValuePair("pass",valuse[1]));
        httpPost.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse httpResponse=  httpClient.execute(httpPost);

        HttpEntity httpEntity=httpResponse.getEntity();
        s= readResponse(httpResponse);

    }
    catch(Exception exception)  {}
    return s;


}
public String readResponse(HttpResponse res) {
    InputStream is=null;
    String return_text="";
    try {
        is=res.getEntity().getContent();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
        String line="";
        StringBuffer sb=new StringBuffer();
        while ((line=bufferedReader.readLine())!=null)
        {
            sb.append(line);
        }
        return_text=sb.toString();
    } catch (Exception e)
    {

    }
    return return_text;


}

1 个答案:

答案 0 :(得分:0)

尝试使用此代码登录您的邮政服务...

private class LoginAysnc extends AsyncTask<Void,Void,Void>
    {
        private ProgressDialog regDialog=null;
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            regDialog=new ProgressDialog(this);
            regDialog.setTitle(getResources().getString(R.string.app_name));
            regDialog.setMessage(getResources().getString(R.string.app_pleasewait));
            regDialog.setIndeterminate(true);
            regDialog.setCancelable(true);
            regDialog.show();
        }       
        @Override
        protected Void doInBackground(Void... params) {
            try 
            {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                        postParameters.add(new BasicNameValuePair("name",
                                yourvaluename));
                        postParameters.add(new BasicNameValuePair("pass",
                                yourvaluepass));

                        String response = null;
                        try {
                            response = SimpleHttpClient
                                    .executeHttpPost("http://192.168.1.218/testdemo/login.php",
                                            postParameters);
                             res = response.toString();

                             return res;

                        } catch (Exception e) {
                            e.printStackTrace();
                            errorMsg = e.getMessage();
                        }
                    }
                }).start();
                try {
                    Thread.sleep(3000);


                //  error.setText(resp);
                    if (null != errorMsg && !errorMsg.isEmpty()) {

                    }
                } catch (Exception e) {
                }

            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            if(regDialog!=null)
            {

                regDialog.dismiss();

                System.out.println("PostId:-"+str_postid);

                }

  // do what u do
    }

SimpleHttpClient.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class SimpleHttpClient {
 /** The time it takes for our client to timeout */
    public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds

    /** Single instance of our HttpClient */
    private static HttpClient mHttpClient;

    /**
     * Get our single instance of our HttpClient object.
     *
     * @return an HttpClient object with connection parameters set
     */
    private static HttpClient getHttpClient() {
    if (mHttpClient == null) {
        mHttpClient = new DefaultHttpClient();
        final HttpParams params = mHttpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
        ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
    }
    return mHttpClient;
    }

    public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
        sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    }
    finally {
        if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }
    }


    public static String executeHttpatch(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
        sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    }
    finally {
        if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }
    }

    /**
     * Performs an HTTP GET request to the specified url.
     *
     * @param url The web address to post the request to
     * @return The result of the request
     * @throws Exception
     */
    public static String executeHttpGet(String url) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
        sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    }
    finally {
        if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
    }
    }
}
相关问题