Java 如何使用http请求获取特定数据?
所以我是android新手,我将制作一个应用程序,获取设备的设备id,将其存储在数据库服务器上,然后检查设备是否与已注册的设备相同,如果是,则转到主活动,如果不是,则需要再次注册。 我的方法:Java 如何使用http请求获取特定数据?,java,android,http,httprequest,Java,Android,Http,Httprequest,所以我是android新手,我将制作一个应用程序,获取设备的设备id,将其存储在数据库服务器上,然后检查设备是否与已注册的设备相同,如果是,则转到主活动,如果不是,则需要再次注册。 我的方法: public class SigninActivity extends AsyncTask<String, String, String> { private Context context; public SigninActivity(Context context, int
public class SigninActivity extends AsyncTask<String, String, String> {
private Context context;
public SigninActivity(Context context, int flag) {
this.context = context;
}
protected void onPreExecute(String result) {
}
//@Override
protected String doInBackground(String... arg0) {
try {
String dev = (String) arg0[0];
String link = "http://10.20.2.14/service_antrian/get_data.php?device_id=" + dev;
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();
Log.d("RETURN", "return");
return sb.toString();
} catch (Exception e) {
Log.d("EXCEPTION", "EXP");
//return "failed";
return new String("Exception: " + e.getMessage());
}
}
// @Override
protected void onPostExecute(String result) {
if (result.equalsIgnoreCase("false")) {
Intent mainIntent = new Intent(UserActivity.this, RegisterActivity.class);
UserActivity.this.startActivity(mainIntent);
} else {
Intent mainIntent = new Intent(UserActivity.this, MainActivity.class);
UserActivity.this.startActivity(mainIntent);
}
}
}
公共类重要性扩展了异步任务{
私人语境;
公共标志性(上下文,int标志){
this.context=上下文;
}
受保护的void onPreExecute(字符串结果){
}
//@凌驾
受保护的字符串doInBackground(字符串…arg0){
试一试{
字符串dev=(字符串)arg0[0];
字符串链接=”http://10.20.2.14/service_antrian/get_data.php?device_id=“+dev;
URL=新的URL(链接);
HttpClient=new DefaultHttpClient();
HttpGet请求=新建HttpGet();
setURI(新URI(link));
HttpResponse response=client.execute(请求);
BufferedReader in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
而((line=in.readLine())!=null){
某人附加(行);
打破
}
in.close();
日志d(“返回”、“返回”);
使某人返回字符串();
}捕获(例外e){
Log.d(“例外”、“经验”);
//返回“失败”;
返回新字符串(“异常:+e.getMessage());
}
}
//@覆盖
受保护的void onPostExecute(字符串结果){
if(result.equalsIgnoreCase(“false”)){
Intent maintent=新的Intent(UserActivity.this、RegisterActivity.class);
UserActivity.this.startActivity(mainIntent);
}否则{
Intent maintent=新的Intent(UserActivity.this,MainActivity.class);
UserActivity.this.startActivity(mainIntent);
}
}
}
这是我的php代码服务:
<?php
ini_set('display_errors', 1);
require_once 'database.php';
if(isset($_GET['device_id'])) {
$device_id = $_GET['device_id'];
$sql = " SELECT * FROM `pasien`.`antrian_mobile` WHERE `device_id`=
'$device_id' ";
$rs = $mysqli->query($sql);
$data = array();
while ($row = $rs->fetch_object()) {
$data[] = $row;
}
if ($mysqli->affected_rows > 0) {
echo "successfull";
} else {
echo "false";
}
echo json_encode($data);
}
?>
您正在从您的服务返回成功
或失败
if ($mysqli->affected_rows > 0) {
echo "successfull";
} else {
echo "failed";
}
但是,您正在将结果与移动应用程序中的false
进行比较,因此它总是采用else路径并打开main活动
if (result.equalsIgnoreCase("false")) {
Intent mainIntent = new Intent(UserActivity.this, RegisterActivity.class);
UserActivity.this.startActivity(mainIntent);
} else {
Intent mainIntent = new Intent(UserActivity.this, MainActivity.class);
UserActivity.this.startActivity(mainIntent);
}
我在您的代码中看到一些问题:
问题#1
活动
不应扩展异步任务
<代码>异步任务
应专用于单个异步任务(如发送HTTP请求),并且活动
表示单个用户屏幕(UI层)。您应该单独创建AsyncTask
,并在如下活动中调用它:
class SigninActivity extends Activity {
// this method should be called in the place where you want to execute your task
// it could be onResume() method, onClick listener for the button or whatever
private void executeAsyncTask() {
new MyTask().execute(...) // put your params here...
}
private class MyTask extends AsyncTask<String, String, String> {
... // your task code goes here...
}
}
类的重要性扩展了活动{
//应该在您想要执行任务的地方调用此方法
//它可以是onResume()方法,也可以是onClick listener以获取按钮或其他内容
私有void executesynctask(){
new MyTask().execute(…)//将参数放在此处。。。
}
私有类MyTask扩展了AsyncTask{
…//您的任务代码在这里。。。
}
}
第2期
如今,在Android应用程序中使用AsyncTask
被认为是一种不好的做法,因为它错误处理能力差,并且不知道活动生命周期。Android上异步操作的推荐解决方案是RxJava(版本2)
第三期
您正在使用HttpClient
。建议使用更成熟的解决方案,如改装Http REST客户端或OkHttp库。它们可以很容易地与RxJava集成
第4期
您正在传递上下文
,但未使用它
第5期
您不必注释@覆盖注释
第6期
你的代码凌乱不堪,难以调试和阅读。一旦你把它弄干净了,解决与之相关的问题就容易多了
第7期
在PHP代码中,您混合了责任
摘要
确保正确使用现有代码,然后尝试改进。可能需要进行逐步调试,以查看错误跳转发生的位置。通过检查相关变量的状态,您应该能够找到缺失的PointEdited,抱歉。但是我早些时候改变了,仍然得到了相同的结果。不用担心,你能记录结果字符串,看看值是多少吗?添加此日志d(“返回”,结果);在此之前,if(result.equalsIgnoreCase(“false”)表示D/result:Exception:length=0;index=0好的,您需要逐一排除故障,我们先检查服务。您使用字符串link=”“+dev下面的log.d(“service”,link)记录服务链接;在您获得该值后,将其复制并粘贴到浏览器中,然后查看您的服务返回的值。您必须首先确保您的服务正常工作。我无法记录服务链接,它直接显示此日志。d(“异常”,“EXP”);是的,谢谢你,我对这个很陌生,我会努力让它变得更好。谢谢你的回答@太好了!