2017-01-03 59 views
2

我是新來的android開發,我想連接到我的api在android中。 我用laravel Framework開發了我的api。 這裏Login.java無法從Android模擬器連接到本地

package com.example.yasha.myapplication; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.concurrent.ExecutionException; 

import android.util.Log; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import javax.net.ssl.HttpsURLConnection; 

public class Login extends AppCompatActivity { 

    EditText emailText; 
    EditText passwordText ; 
    TextView signup; 
    Button loginButton ; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     emailText = (EditText) findViewById(R.id.email); 
     passwordText = (EditText) findViewById(R.id.password); 
     signup = (TextView) findViewById(R.id.signup); 
     loginButton = (Button) findViewById(R.id.login); 

     signup.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Start the Signup activity 
       Intent signupIntent = new Intent(getApplicationContext(), SignUp.class); 
       startActivity(signupIntent); 
      } 
     }); 


    } 

    public class MakeAccessToken extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... urls) { 

      String result = ""; 
      URL url; 
      HttpURLConnection urlConnection = null; 

      try { 
       url = new URL(urls[0]); 

       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("POST"); 
       InputStream in = urlConnection.getInputStream(); 

       InputStreamReader reader = new InputStreamReader(in); 

       int data = reader.read(); 

       while (data != -1) { 

        char current = (char) data; 

        result += current; 

        data = reader.read(); 

       } 

       //int responseCode = urlConnection.getResponseCode(); 
       //if(responseCode == HttpsURLConnection.HTTP_OK){ 
        return result; 
       //}else{ 
       // return "Wrong Creditinal"; 
       // } 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      Log.i("Access Token", result); 
     } 
    } 

    public void login(View view) { 

     if (!validate()) { 
      onLoginFailed(); 
      return; 
     } 
     final String email = emailText.getText().toString(); 
     final String password = passwordText.getText().toString(); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
     final int client_id = prefs.getInt("client_id", 1); 
     final String client_secret = prefs.getString("client_secret", " "); 


     loginButton.setEnabled(false); 

     final ProgressDialog progressDialog = new ProgressDialog(this); 
     progressDialog.setIndeterminate(true); 
     progressDialog.setMessage("Verifying..."); 
     progressDialog.show(); 

     // TODO: Implement your own authentication logic here. 
     new android.os.Handler().postDelayed(
       new Runnable() { 
        public void run() { 
         MakeAccessToken task = new MakeAccessToken(); 
         task.execute("http://10.0.2.3:8000/oauth/token?grant_type=password&client_id="+client_id+"&client_secret="+client_secret+"&username="+email+"&password="+password); 
        } 
       }, 3000); 

    } 



    public void onLoginSuccess() { 
     loginButton.setEnabled(true); 
     Intent profileIntent = new Intent(getApplicationContext(), Profile.class); 
     startActivity(profileIntent); 
    } 

    public void onLoginFailed() { 
     Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); 

     loginButton.setEnabled(true); 
    } 

    public boolean validate() { 
     boolean valid = true; 

     String email = emailText.getText().toString(); 
     String password = passwordText.getText().toString(); 

     if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { 
      emailText.setError("enter a valid email address"); 
      valid = false; 
     } else { 
      emailText.setError(null); 
     } 

     if (password.isEmpty() || password.length() < 4 || password.length() > 10) { 
      passwordText.setError("between 4 and 10 alphanumeric characters"); 
      valid = false; 
     } else { 
      passwordText.setError(null); 
     } 

     return valid; 
    } 
} 

我的API存儲在本地。我在Windows中使用xampp。 Laravel項目在8000港口上運行。 我的代碼是正確的,因爲我可以連接到網絡上的其他api。 我能夠連接到本地主機但無法訪問8000端口。 我嘗試了很多方法,如使用10.10.2.2,10.10.2.3。 但獲取錯誤

java.net.ConnectException: failed to connect to /10.0.2.2 (port 8000): connect failed: ETIMEDOUT 

任何幫助表示讚賞。請幫助我,我想這樣做3天。

回答

2

你需要使用該系統的IP地址你的Laravel項目正在運行。本地主機只能用於連接到同一系統內的服務器。

+0

我laravel項目是在同一系統上,我已經使用的IP地址 – Yash

+0

你有沒有嘗試過改變任何文件httpd.conf文件允許從所有IP地址連接? – dpaksoni

+0

你能告訴怎麼做 – Yash

0
You can have an alternative as SOAP with this having your code in PHP connect to your localhost and your code in android just calls the method to your PHP code. 

在PHP代碼:

<?php 
class service{  
    public function getUser(){ 
     $user = "root";`enter code here` 
     $pass = ""; 
     $database = ""; 
     $server = "localhost"; 
     $mysqli = new mysqli($server,$user,$pass,$database); 
     $vquery = "Your query here";     
     //<Your code in iterating the data you can put it as JSON > 
     $mysqli->close(); 
     return $<return JSON user data>; 
    } 
} 
$server = new SoapServer(null, array(
'uri' => "urn://<url>", 
'soap_version' => SOAP_1_2) 
);    
$server->setClass("service");  
$server->handle(); 
?> 

Android中: 參考您的代碼在這裏 AsyncTask Android example

Hope this helps! 
+0

我無法理解你的意思 – Yash

+0

什麼有這個替代的解決方案,它是從Android上的SOAP調用你的方法,以PHP代碼嵌入你的laravel文件夾php方法然後查詢到你的本地主機。 – 2017-01-03 09:31:56

+0

Laravel是php的一個框架,沒有 – Yash

相關問題