gpt4 book ai didi

java - 将数据从 Android 应用程序发送到 cakephp 的whith url

转载 作者:行者123 更新时间:2023-12-01 14:14:24 25 4
gpt4 key购买 nike

我编写了一个 Android 登录应用程序,可以从用户那里获取用户名和密码。我想将用户名和密码发送到 cakephp 应用程序服务器。这是我的 Android 代码:

    httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/ar/users/login?name="+name+"&password="+pass);

//Execute HTTP Post Request
response=httpclient.execute(httppost);
final ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
JSONArray jArray;
jArray = new JSONArray(result);
JSONObject j =new JSONObject();
j = jArray.getJSONObject(0);
JSONObject json = j.getJSONObject("User");
text = json.getString("last_name");

我在布局中只写了这一行:

         <?php   echo $content_for_layout ?>

查看代码:

        <?php echo json_encode($user); ?>

Controller :

    function login() {
if(isset($this->params['url']['name']))
$data = $this -> User -> findByuser_name($this->params['url']['name']);

if ($data && $data['User']['password'] == ($this->params['url']['password'])) {

$this -> set('user', $data);

} else {
$this -> set('error', true);
}
}

通过在浏览器中输入以下网址,此代码可以很好地工作,但在 Android 应用程序中不起作用!“本地主机/ar/用户/登录?名称=m_sepehri&密码=111”有人可以帮助我吗?

最佳答案

您正在使用 httppost,但附加您的数据名称并传递到 url 本身。将 AsyncTask 与 List 一起使用,如下所示:

private class MyAsyncTask extends AsyncTask<String, Integer, Double> {

@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(name1, email1, password1, mobile1);
return null;
}

protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),
"Account Activated Login To MyGenie",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
}

protected void onProgressUpdate(Integer... progress) {
pb.setProgress(progress[0]);
}

public void postData(String name, String email, String password,
String mobile) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://yoursite.php");

try {
// Add your data
List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", name));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs
.add(new BasicNameValuePair("password", password));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

}

关于java - 将数据从 Android 应用程序发送到 cakephp 的whith url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com