2016-06-24 4 views
-3

Я новичок в 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; 


} 
+0

всего "Http ......" это в красном color – chandu

+0

Обычно есть описание ошибки или сообщения об ошибке. Зная это, мы можем вам помочь. Без этого мы можем только догадываться. Вы забыли импортировать классы? –

+0

Сообщите нам, какую ошибку вы получаете, поэтому мы можем вам помочь. – SripadRaj

ответ

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

 Смежные вопросы

  • Нет связанных вопросов^_^