gpt4 book ai didi

java - 似乎无法让从我的 Android 应用程序发送的 http 获取参数显示在我的 PHP 页面上

转载 作者:可可西里 更新时间:2023-11-01 17:34:31 24 4
gpt4 key购买 nike

我似乎无法让我的 PHP 页面显示我在 Android 中使用 http 客户端发送的数据。我现在需要的只是用 PHP 显示它,这似乎是一个挑战,我知道我做错了什么。

任何指导将不胜感激。我已经尝试了从 var_dump($_SERVER)json_decode 的所有方法以在 PHP 中显示它。甚至可以在 PHP 页面上显示它吗?

     private class Connection extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects){
try{
PostData(R.id.fullscreen_content, 3);
}
catch(IOException exception){
exception.printStackTrace();
}
return null;
}
}

protected void PostData(Integer Question_ID,Integer ResponseChosen_ID) {

URL url = new URL("http://10.0.2.2:443/SwlLogin.php");
HttpURLConnection conn =(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);

HttpClient httpClient = new DefaultHttpClient();
HttpGet post = new HttpGet(conn.getURL().toString());
post.setHeader("Content-type","application/json");

conn.connect();
Date date = new Date();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd", Locale.UK);
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss",Locale.UK);

String nowDate = dt.format(date);
String nowTime = time.format(date);

String phpDate = nowDate;
String phpTime = nowTime;


ArrayList<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("Question ID", Question_ID.toString()));
params.add(new BasicNameValuePair("Response_Chosen_ID", ResponseChosen_ID.toString()));
params.add(new BasicNameValuePair("TimestampDate", phpDate));
params.add(new BasicNameValuePair("time", phpTime));

JSONArray array = new JSONArray();
array.put(params);
post.setHeader("postData", params.toString());
post.getParams().setParameter("JSON", params);
HttpParams var = httpClient.getParams();
var.setParameter("GET",params);

HttpResponse response = httpClient.execute(post);
OutputStreamWriter write = new OutputStreamWriter(conn.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder builder = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
builder.append(line);
}
Log.d("Response:", builder.toString());
builder.toString();
reader.close();

public void happy_click(View view) throws IOException {
try{
new Connection().execute();
report_success();
}
catch(Exception exception){
messageBox("Response was not successful","Failed to process response" + exception.getMessage());
}
}

最佳答案

您不能在 UI 线程上运行此代码,否则您将获得 NetworkRequestOnUIThread 异常。你必须在不同的线程上执行此操作。

通过尝试使用 AsyncTask

 private class Uploader extends AsyncTask<Void,Void,Void>{
protected void doInBackground(){
// do network request here
}
private void onPostExecute(){
// handle UI updates here as is on ui Thread
}
}

或者您可以使用我强烈推荐的 OkHTTP 库。为此,请从 okHttp 下载 jar。 .将它添加到你的 libs 文件夹然后你可以像这样进行网络调用

  MediaType JSON = MediaType.parse("application/json; charset=utf-8");
JSONObject parcel = new JSONObject();
try{
parcel.put("email", emailEdit.getText().toString());
parcel.put("password", passwordEdit.getText().toString());
parcel.put("device", "Android");
parcel.put("hash", "1234");;
}catch (JSONException e ){
e.printStackTrace();
}
RequestBody body = RequestBody.create(JSON, parcel.toString());
Request request = new Request.Builder()
.header("Content-Type", "application/json")
.url("YOURURL")
.post(body)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if (null != e) {e.printStackTrace();}
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (null != response && response.message().equals(Constants.KEY_OK)) {
JSONObject serverResponse;
try{
serverResponse = new JSONObject(response.body().string());
if(serverResponse.getBoolean(Constants.KEY_SUCCESS)){
Constants.getInstance().setToken(serverResponse.getString(Constants.KEY_TOKEN));
moveToHomePage();
}else{
showLoginFail();
}

}catch (JSONException e ){
e.printStackTrace();
}
response.body().close();
} else {
showLoginFail();
}
}
});

还要确保你有

 <uses-permission android:name="...permision.INTERNET"> 

在你的 list 文件中

关于java - 似乎无法让从我的 Android 应用程序发送的 http 获取参数显示在我的 PHP 页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35512161/

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