doubao6464 2018-09-03 16:51 采纳率: 100%
浏览 235
已采纳

如何从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 2018-09-04 13:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘