Im beginner in this but I can't read json data from my laravel app on artisan localhost to android app every time I got this even with changing http://localhost:8000/jlogin to http://ift.tt/1S1jt02 or even with my ip address
java.net.ConnectException: faild to connect to /10.0.2.2 (port 8000); connect failed:ETIMEDOUT (Connection timed out)
this is my mainActivity :
package com.example.myapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
EditText email, password;
String Email, Password;
Context ctx=this;
String NAME=null, PASSWORD=null, EMAIL=null;
Button loginbtn;
TextView donetextview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
email = (EditText) findViewById(R.id.editText4);
password = (EditText) findViewById(R.id.editText1);
loginbtn=(Button) findViewById(R.id.button1);
donetextview = (TextView) findViewById(R.id.textView1);
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Email = email.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Email, Password);
}
});
}
/////////////////////////////////
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;
try {
URL url = new URL("http://ift.tt/1S1jt02");
//URL url = new URL("http://localhost:8000/jlogin");
String urlParams = "email="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception : "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception : "+e.getMessage();
}
}
///
protected void onPostExecute(String s) {
String err=null;
try {
JSONObject root = new JSONObject(s);
JSONObject user_data = root.getJSONObject("user_data");
NAME = user_data.getString("name");
PASSWORD = user_data.getString("password");
EMAIL = user_data.getString("email");
} catch (JSONException e) {
e.printStackTrace();
err = "Exception: "+e.getMessage();
}
//Intent i = new Intent(ctx, Home.class);
//i.putExtra("name", NAME);
//i.putExtra("password", PASSWORD);
//i.putExtra("email", EMAIL);
//i.putExtra("err", err);
//startActivity(i);
donetextview.setText(EMAIL); // just to test
}
}
/////////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire