gpt4 book ai didi

java - 来自 Android 客户端的 HTTP 请求

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

我有一个在本地主机上运行的非常基本的 Spring Boot 应用程序,我正在尝试从 android studio 上的另一个应用程序获取一些数据。我的端点如下所示:http://localhost:8080/company/ {id} 并返回:

My data

在我的 Android 应用程序中,我创建了一个包装类:

package gr.games.panayiotis.myapplication;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Company {

@JsonProperty("id")
private long id;
@JsonProperty("name")
private String name;


public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

在主应用程序中,我正在创建一个名为 Task 的新类,它扩展 AsyncTask 来获取 api:

package gr.games.panayiotis.myapplication;

import android.os.AsyncTask;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class MainActivity extends AppCompatActivity {


public class Task extends AsyncTask<String, Void, ResponseEntity<Company>>{


@Override
protected ResponseEntity<Company> doInBackground(String... strings) {
String url = strings[0];
RestTemplate restTemplate = new RestTemplate();

try{
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());


HttpHeaders headers = new HttpHeaders();

headers.add("Content-Type", "application/json");

HttpEntity<String> entity = new HttpEntity<String>(headers);

int id = 1;
ResponseEntity<Company> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, Company.class, id);

System.out.println(responseEntity.getStatusCode());
System.out.println(responseEntity.toString());

return responseEntity;

}catch (Exception e){
//System.out.println("ERROR:\n");
e.getMessage();
//System.out.println("\nEND OF ERROR");
return null;
}
}

protected void onPostExecute(ResponseEntity<Company> response){
//Company company = response.getBody();
// System.out.println(company.getName());
}
}


//A button invokes this method
public void getData(View view){
String url = "http://localhost:8080/company/1";
new Task().execute(url);
}



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

在 doInBackground() 方法中,我的代码运行到我调用restTemplate.exchange() 的位置。之后,它进入 catch block 并向响应返回 null。

第一次调用 getData() 方法时,我收到这些警告:

Capturing and displaying logcat messages from application. This behavior can be disabled in the 
"Logcat output" section of the "Debugger" settings page.
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/Java7Handlers: Unable to load JDK7 types (java.nio.file.Path): no Java7 type support added

我没有收到任何错误,但我不明白为什么我没有得到回复。我的 Spring Boot 应用程序从未收到请求。我刚刚接触 Android,所以非常感谢任何帮助。

最佳答案

在您的 AndroidManifest.xml 中添加此行,android:allowClearUserData="true"..也许它会解决您的问题。

 <application
android:allowBackup="true"
android:allowClearUserData="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

或从下面的链接引用我的方法..

https://stackoverflow.com/questions/59464257/login-using-url-with-username-and-password-in-android-mobile-app/59466586#59466586

关于java - 来自 Android 客户端的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59436725/

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