Я попытался подключиться к своему локальному xampp с помощью android studio, используя класс AsyncTask.Вот AsyncTask, но он выдает ошибку, говоря: Failed to connect
to /10.0.2.2:800
Я попробовал свой реальный IP-адрес cmd -> ipconfig IPV4 и некоторые другие приемы, но они не увенчались успехом
Это мойструктура приложения
AsyncTask с использованием класса
package app.buil.land.food.doymaj.doymaj;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by ProgrammingKnowledge on 1/5/2016.
*/
public class backGroundActivities extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
backGroundActivities (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.0.2.2:800/login.php";
if(type.equals("login")) {
try {
String phone_number = params[1];
// String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("phone_number","UTF-8")+"="+URLEncoder.encode(phone_number,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
Log.e("Line_from_php_server",result);
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
Вот мой MainActivity
package app.buil.land.food.doymaj.doymaj;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText phone_Number;
EditText user_Name;
EditText pass_Word;
Button singButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// singButton = (Button)findViewById(R.id.sing_id);
// singButton.setOnClickListener(kyListener);
phone_Number =(EditText)findViewById(R.id.mobile_id);
user_Name =(EditText)findViewById(R.id.name_id);
pass_Word =(EditText)findViewById(R.id.p_id);
}
// Push_Identity db = new Push_Identity(this,"CustomerDatabas.db",null,1);
public boolean validate(EditText[] fields){
for(int i = 0; i < fields.length; i++){
EditText currentField = fields[i];
if(currentField.getText().toString().length() <= 0){
switch (i){
case 0:
Toast.makeText(getApplicationContext(),"شماره تلفن وارد نشده ",Toast.LENGTH_LONG).show() ;
break;
case 1:
Toast.makeText(getApplicationContext(),"نام کاربری وارد نشده ",Toast.LENGTH_LONG).show() ;
break;
case 2:
Toast.makeText(getApplicationContext(),"رمز وارد نشده ",Toast.LENGTH_LONG).show() ;
break;
}
return false;
}
}
return true;
}
//private View.OnClickListener kyListener = new View.OnClickListener() {
public void send_number(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know
boolean fieldsOK = validate(new EditText[] { phone_Number, user_Name, pass_Word });
if (fieldsOK ==true){
String Phon = phone_Number.getText().toString();
Toast.makeText(getApplicationContext(),Phon,Toast.LENGTH_LONG);
// the Tooast dose not display anything
// it seems that the EditText is empty buy i enter the value in
// in it.
String type ="login";
backGroundActivities back = new backGroundActivities(this);
back.execute(type,Phon);
//
}
}
// };
}
XML
<EditText
android:id="@+id/mobile_id"
android:layout_width="match_parent"
android:layout_height="43dp"
android:background="@drawable/customized_edtitext"
android:drawablePadding="15dp"
android:paddingRight="10dp"
android:paddingLeft="15dp"
android:drawableRight="@drawable/ic_smartphone"
android:elevation="2dp"
android:textStyle="bold"
android:hint="@string/register_phone_commnet"
android:fontFamily="@font/iransansmedium"
android:textSize="12sp"
/>
<EditText
android:id="@+id/name_id"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_below="@id/mobile_id"
android:background="@drawable/customized_edtitext1"
android:drawablePadding="15dp"
android:paddingRight="10dp"
android:paddingLeft="15dp"
android:drawableRight="@mipmap/ic_user"
android:elevation="2dp"
android:textStyle="bold"
android:hint="@string/register_user_commnet"
android:fontFamily="@font/iransansmedium"
android:textSize="12dp" />
<EditText
android:id="@+id/p_id"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_below="@+id/name_id"
android:background="@drawable/customized_edtitext2"
android:drawablePadding="15dp"
android:paddingRight="10dp"
android:paddingLeft="15dp"
android:drawableRight="@mipmap/ic_unlocked"
android:elevation="2dp"
android:textStyle="bold"
android:hint="@string/register_pass_commnet"
android:fontFamily="@font/iransansmedium"
android:textSize="12sp"
android:paddingEnd="10dp" />
вот URL моей страницы php введите описание изображения здесь
И еще немного информации: у меня установлен Genymotion, а также bluestackи VirtaulBox.Они портятся из-за порта?Я имею в виду, могут ли эти программы вызывать конфликтующие порты Apache xampp и их порты?