1. AC Question's Avatar
    I want to connect mySql data base in android but it does not .
    *here is my code to connect with php file to insert data
    .......

    package com.broken.nezamulislamar.mysqlblog_1;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.widget.TextView;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.URI;
    import java.net.URL;
    import java.net.URLConnection;
    import java.net.URLEncoder;

    public class SigninActivity extends AsyncTask<String,Void,String>{
    private TextView statusField,roleField;
    private Context context;
    private int byGetOrPost = 0;

    //flag 0 means get and 1 means post.(By default it is get.)
    public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
    this.context = context;
    //this.statusField = statusField;
    //this.roleField = roleField;
    byGetOrPost = flag;
    }

    protected void onPreExecute(){

    }

    @Override
    protected String doInBackground(String... arg0) {
    if(byGetOrPost == 0){ //means by Get Method

    try{
    String username = (String)arg0[0];
    String password = (String)arg0[1];
    String link = "http://127.0.0.1/Client/jakir.php?username="+username+"& password="+password;

    URL url = new URL(link);
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(link));
    HttpResponse response = client.execute(request);
    BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer sb = new StringBuffer("");
    String line="";

    while ((line = in.readLine()) != null) {
    sb.append(line);
    break;
    }
    in.close();
    return sb.toString();
    }

    catch(Exception e){
    return new String("Exception: " + e.getMessage());
    }
    }
    else{
    try{
    String username = (String)arg0[0];
    String password = (String)arg0[1];

    String link="http://127.0.0.1/Client/jakir.php?";
    String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
    data += "&" + URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

    URL url = new URL(link);
    URLConnection conn = url.openConnection();

    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    wr.write( data );
    wr.flush();

    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    StringBuilder sb = new StringBuilder();
    String line = null;

    // Read Server Response
    while((line = reader.readLine()) != null)
    {
    sb.append(line);
    break;
    }
    return sb.toString();
    }
    catch(Exception e){
    return new String("Exception: " + e.getMessage());
    }
    }
    }

    @Override
    protected void onPostExecute(String result){
    this.statusField.setText("Login Successful");
    this.roleField.setText(result);
    }
    }

    *here is the exception it shows
    ....
    Exception: failed to connect to /127.0.0.1(port80):connect failed: ECONNREFUSED(Connection Refused)
    06-11-2016 09:36 AM

Similar Threads

  1. My Galaxy s7 keeps dropping calls in my house.
    By AC Question in forum Ask a Question
    Replies: 0
    Last Post: 06-11-2016, 09:28 AM
  2. Replies: 0
    Last Post: 06-11-2016, 09:25 AM
  3. Replies: 2
    Last Post: 06-11-2016, 09:15 AM
  4. Replies: 1
    Last Post: 06-11-2016, 09:14 AM
  5. where can I find my APN settings on my android 5.1
    By AC Question in forum Ask a Question
    Replies: 1
    Last Post: 06-11-2016, 09:03 AM
LINK TO POST COPIED TO CLIPBOARD