2016-06-24 12 views
-3

Ich bin neu in Android, ich entwickle meine eigene App. In diesem möchte ich eine Verbindung zur Datenbank herstellen. Ich habe ein Tutorial gefunden und den kompletten Code verfolgt, aber es funktioniert nicht. Auf diesem Code bekomme ich farbigen roten Text.Wie kann ich Login-Daten in der Datenbank speichern ....?

 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

in all "Http ......" es in rot Farbe – chandu

+0

Normalerweise gibt es eine Beschreibung des Fehlers oder irgendeine Art von Fehlermeldung. Wenn wir es wissen, können wir Ihnen helfen. Ohne es können wir nur raten. Hast du vergessen, Klassen zu importieren? –

+0

Lassen Sie uns wissen, welchen Fehler Sie bekommen, damit wir Ihnen helfen können. – SripadRaj

Antwort

0

Versuchen Sie diesen Kodex für die Anmeldung in Ihrem Postservice ...

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