doubao6464
doubao6464
2018-09-03 16:51

如何从android studio向本地数据库发送输入信息

I'm starter with Android and I'm trying an example to get some information from an edittext and send that to my localhost and if my Localdatabase isset that so echo something, but whatever I did, it doesn't work and OutputStreamWriter work in Android Studio when I check it with debugger but nothing send to my local database.

Below I share my codes. The error is that $_POST["email"] doesn't work and errors that email is undefined?!

//It's my MainActivity's codes

public class MainActivity extends AppCompatActivity {
Button show;
TextView txt;
EditText edt;
public static Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context=MainActivity.this;
    edt=(EditText)findViewById(R.id.edt);
    show=(Button)findViewById(R.id.show);
    txt=(TextView)findViewById(R.id.txt);

    show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new AsynctaskTest("http://152.144.1.215/sadegh",edt.getText().toString()).execute();

        }
    });
}

}

here is my AsynctaskGetData's codes:

public class AsynctaskTest extends AsyncTask<String,Integer,String>{

public String link="";
public String email="";
public StringBuilder builder;
ProgressDialog progressDialog;

public AsynctaskTest(String link,String email){
    this.link=link;
    this.email=email;

}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog=new ProgressDialog(MainActivity.context);
    progressDialog.setTitle("Processing...");
    progressDialog.setMessage("Please Wait...");
    progressDialog.show();
}

@Override
protected String doInBackground(String... strings) {
    try {
        String data=URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8");
        URL url=new URL(link);
        URLConnection connection=url.openConnection();

        connection.setDoOutput(true);
        OutputStreamWriter writer=new OutputStreamWriter(connection.getOutputStream());
        writer.write(data);
        writer.flush();

        BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
        builder=new StringBuilder();
        String line=null;
        while ((line=reader.readLine())!=null){
            builder.append(line);
        }


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    progressDialog.dismiss();
    Toast.makeText(MainActivity.context, s, Toast.LENGTH_SHORT).show();
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
}

}

It's my php code

<?php
$email=$_POST["email"];

echo "hello"
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhan1238 douzhan1238 3年前

    Wow , I'm really glad to find the reason of this problem that nothing sent to my local database from android studio this problem is because of this code in MainActivity :

    new AsynctaskTest("http://152.144.1.215/sadegh",edt.getText().toString()).execute();
    

    we should after /sadegh type a page that we want to sent information to that like this:

    new AsynctaskTest("http://152.144.1.215/sadegh/index.php",edt.getText().toString()).execute();
    

    I hope my this problem and this answer help another people.

    goodluck guys.

    点赞 评论 复制链接分享