2016-01-08 103 views
0

我在android studio(toni kami教程)上登錄/註冊了一個教程,我完成了他所做的一切,但httpclient/httpparams方法不再適用於android studio,即時通訊新的應用程序編程,所以我搜索互聯網的方式來解決這個問題,我發現了一個url連接代碼,我複製它,但它不工作。網絡主機上的外部數據庫連接不工作

******這是所有的java類****** ****** ------幫我糾正這個問題或給我一個新的想法來解決它------ 當我運行應用程序,我得到這樣的:

package com.thenewboston.loginr; 
 

 
import android.app.ProgressDialog; 
 
import android.content.Context; 
 
import android.os.AsyncTask; 
 
import android.util.Log; 
 

 
import java.io.BufferedReader; 
 
import java.io.IOException; 
 
import java.io.InputStreamReader; 
 
import java.io.OutputStreamWriter; 
 
import java.io.UnsupportedEncodingException; 
 
import java.net.HttpURLConnection; 
 
import java.net.URL; 
 
import java.net.URLEncoder; 
 
import java.util.HashMap; 
 
import java.util.Map; 
 

 
/** 
 
* Created by sleeman on 1/1/16. 
 
*/ 
 
public class ServerRequests { 
 

 
    ProgressDialog progressDialog; 
 
    public static final int CONNECTION_TIMEOUT = 1000 * 15; 
 
    public static final String SERVER_ADDRESS = "http://sleemanb94.tk/"; 
 

 
    public ServerRequests(Context context){ 
 
     progressDialog = new ProgressDialog(context); 
 
     progressDialog.setCancelable(false); 
 
     progressDialog.setTitle("Processing"); 
 
     progressDialog.setMessage("Please Wait..."); 
 

 
    } 
 

 
    public void storeUserDataInBackground(User user, getUserCallBack userCallback){ 
 
     progressDialog.show(); 
 
     new StoreUserDataAsyncTask(user,userCallback).execute(); 
 
    } 
 

 
    public void fetchUserDataInBackground(User user, getUserCallBack callback) { 
 
     progressDialog.show(); 
 
     new fetchUserDataAsyncTask(user, callback).execute(); 
 

 
    } 
 

 
    public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void>{ 
 
     User user; 
 
     getUserCallBack userCallback; 
 

 
     public StoreUserDataAsyncTask(User user, getUserCallBack userCallback){ 
 
      this.user = user; 
 
      this.userCallback = userCallback; 
 
     } 
 

 
     @Override 
 
     protected Void doInBackground(Void... params) { 
 
      Map<String,String> dataToSend = new HashMap<>(); 
 
      dataToSend.put("fname", user.fname); 
 
      dataToSend.put("lname", user.lname); 
 
      dataToSend.put("email", user.email); 
 
      dataToSend.put("password", user.password); 
 

 
      //Server Communication part - it's relatively long but uses standard methods 
 

 
      //Encoded String - we will have to encode string by our custom method (Very easy) 
 
      String encodedStr = getEncodedData(dataToSend); 
 

 
      //Will be used if we want to read some data from server 
 
      BufferedReader reader = null; 
 

 
      //Connection Handling 
 
      try { 
 
       //Converting address String to URL 
 
       URL url = new URL(SERVER_ADDRESS + "Register.php"); 
 
       //Opening the connection (Not setting or using CONNECTION_TIMEOUT) 
 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
 

 
       //Post Method 
 
       con.setRequestMethod("POST"); 
 
       //To enable inputting values using POST method 
 
       //(Basically, after this we can write the dataToSend to the body of POST method) 
 
       con.setDoOutput(true); 
 
       OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); 
 
       //Writing dataToSend to outputstreamwriter 
 
       writer.write(encodedStr); 
 
       //Sending the data to the server - This much is enough to send data to server 
 
       //But to read the response of the server, you will have to implement the procedure below 
 
       writer.flush(); 
 

 
       //Data Read Procedure - Basically reading the data comming line by line 
 
       StringBuilder sb = new StringBuilder(); 
 
       reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
 

 
       String line; 
 
       while((line = reader.readLine()) != null) { //Read till there is something available 
 
        sb.append(line + "\n");  //Reading and saving line by line - not all at once 
 
       } 
 
       line = sb.toString();   //Saving complete data received in string, you can do it differently 
 

 
       //Just check to the values received in Logcat 
 
       Log.i("custom_check", "The values received in the store part are as follows:"); 
 
       Log.i("custom_check",line); 
 

 
      } catch (Exception e) { 
 
       e.printStackTrace(); 
 
      } finally { 
 
       if(reader != null) { 
 
        try { 
 
         reader.close();  //Closing the 
 
        } catch (IOException e) { 
 
         e.printStackTrace(); 
 
        } 
 
       } 
 
      } 
 

 
      return null; 
 

 
     } 
 

 
     private String getEncodedData(Map<String,String> data) { 
 
      StringBuilder sb = new StringBuilder(); 
 
      for(String key : data.keySet()) { 
 
       String value = null; 
 
       try { 
 
        value = URLEncoder.encode(data.get(key), "UTF-8"); 
 
       } catch (UnsupportedEncodingException e) { 
 
        e.printStackTrace(); 
 
       } 
 

 
       if(sb.length()>0) 
 
        sb.append("&"); 
 

 
       sb.append(key + "=" + value); 
 
      } 
 
      return sb.toString(); 
 
     } 
 
     @Override 
 
     protected void onPostExecute(Void aVoid) { 
 
      progressDialog.dismiss(); 
 
      userCallback.done(null); 
 

 
      super.onPostExecute(aVoid); 
 
     } 
 
    } 
 

 
    public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> { 
 
     User user; 
 
     getUserCallBack userCallback; 
 

 
     public fetchUserDataAsyncTask(User user, getUserCallBack userCallback) { 
 
      this.user = user; 
 
      this.userCallback = userCallback; 
 
     } 
 

 
     @Override 
 
     protected User doInBackground(Void... params) { 
 

 
      //Use HashMap, it works similar to NameValuePair 
 
      Map<String, String> dataToSend = new HashMap<>(); 
 
      dataToSend.put("email", user.email); 
 
      dataToSend.put("password", user.password); 
 

 
      //Server Communication part - it's relatively long but uses standard methods 
 

 
      //Encoded String - we will have to encode string by our custom method (Very easy) 
 
      String encodedStr = getEncodedData(dataToSend); 
 

 
      //Will be used if we want to read some data from server 
 
      BufferedReader reader = null; 
 

 
      //Connection Handling 
 
      User returnedUser = null; 
 
      try { 
 
       //Converting address String to URL 
 
       URL url = new URL(SERVER_ADDRESS + "FetchUserData.php"); 
 
       //Opening the connection (Not setting or using CONNECTION_TIMEOUT) 
 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
 

 
       //Post Method 
 
       con.setRequestMethod("POST"); 
 
       //To enable inputting values using POST method 
 
       //(Basically, after this we can write the dataToSend to the body of POST method) 
 
       con.setDoOutput(true); 
 
       OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); 
 
       //Writing dataToSend to outputstreamwriter 
 
       writer.write(encodedStr); 
 
       //Sending the data to the server - This much is enough to send data to server 
 
       //But to read the response of the server, you will have to implement the procedure below 
 
       writer.flush(); 
 

 
       //Data Read Procedure - Basically reading the data comming line by line 
 
       StringBuilder sb = new StringBuilder(); 
 
       reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
 

 
       String line; 
 
       while ((line = reader.readLine()) != null) { //Read till there is something available 
 
        sb.append(line + "\n");  //Reading and saving line by line - not all at once 
 
       } 
 
       line = sb.toString();   //Saving complete data received in string, you can do it differently 
 

 
       //Just check to the values received in Logcat 
 
       Log.i("custom_check", "The values received in the store part are as follows:"); 
 
       Log.i("custom_check", line); 
 
       returnedUser = new User(user.fname, user.lname); 
 

 
      } catch (Exception e) { 
 
       e.printStackTrace(); 
 
      } finally { 
 
       if (reader != null) { 
 
        try { 
 
         reader.close();  //Closing the 
 
        } catch (IOException e) { 
 
         e.printStackTrace(); 
 
        } 
 
       } 
 
      } 
 
      return returnedUser; 
 
     } 
 
     @Override 
 
     protected void onPostExecute(User returnedUser) { 
 
      progressDialog.dismiss(); 
 
      userCallback.done(returnedUser); 
 

 
      super.onPostExecute(returnedUser); 
 
     } 
 

 
     private String getEncodedData(Map<String, String> data) { 
 
      StringBuilder sb = new StringBuilder(); 
 
      for (String key : data.keySet()) { 
 
       String value = null; 
 
       try { 
 
        value = URLEncoder.encode(data.get(key), "UTF-8"); 
 
       } catch (UnsupportedEncodingException e) { 
 
        e.printStackTrace(); 
 
       } 
 

 
       if (sb.length() > 0) 
 
        sb.append("&"); 
 

 
       sb.append(key + "=" + value); 
 
      } 
 
      return sb.toString(); 
 
     } 
 

 

 

 
    } 
 
}

+0

任何有錯誤或什麼?讓我們看看關於你試用的更多信息 –

+0

即時消息應用程序編程,我從youtube上的教程複製代碼,但與Web主機(有數據庫)的連接方法(httpclient)不再工作,谷歌和Android決定阻止該方法,所以我複製了一個url連接代碼,但它不工作,我在這裏發佈了所有的代碼,我希望你看看它,並診斷問題,也許是它的一個小問題。 –

+0

也許這個教程可以幫助你http://www.vogella.com/tutorials/AndroidJSON/article.html#json_android –

回答

0

HttpClient已被棄用。但你可以使用它在Android工作室很容易

Check this answer

相關問題