我通过.ne
t核心 API 提供了一些休息服务。我在Android应用程序中使用以下代码对其进行了调用。有时服务工作并返回了数据,但有时我却得到了status code 200
但没有返回任何值。我很迷惑!我不知道发生了什么,为什么具体方法有时行得通,有时行不通!
服务:
[Route("api/[controller]")]
[ApiController]
public class PMServicesController : BaseController
{
private readonly IPMServices pmService;
public PMServicesController(
IPMServices pmService)
{
this.pmService = pmService;
}
[Route("Test"), HttpPost]
public string set(string test)
{
return "Test is successfull";
}
}
在android中:
@Override
protected String doInBackground(Object... params) {
try {
String uri = serviceURL + "/" + methodName;
HttpConnectionParams.setConnectionTimeout(httpParameters,
connectionTimeout);
HttpConnectionParams.setSoTimeout(httpParameters, socketTimeOut);
httpClient.setParams(httpParameters);
HttpResponse response = null;
this.requestType = HttpRequestType.GET;
if (params.length == 1) {
this.requestType = HttpRequestType.POST;
}
/*if (params.length > 1)
if (params[0] != null)
this.requestType = HttpRequestType.POST;*/
if (this.requestType == HttpRequestType.POST) {
// /// Input parameter exists /////////////
//this.jsonStringer = getJsonStringFromObject(params);
this.jsonStringer=params[0].toString();
HttpPost request = new HttpPost(uri);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
StringEntity msg = new StringEntity(jsonStringer,HTTP.UTF_8);
msg.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
msg.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
request.setEntity(msg);
response = httpClient.execute(request);
} else {
uri = uri ;//+ "?" + params[0];
HttpGet request = new HttpGet(uri);
response = httpClient.execute(request);
}
/*int b;
if(methodName.equalsIgnoreCase("PostCurrent"))
b = response.getStatusLine().getStatusCode();*/
int r = response.getStatusLine().getStatusCode();
isCommunicationSucceed = r == 200;
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
int contentLength = (int) responseEntity.getContentLength();
String result = "";
// if (r == 200) {
if (contentLength > 0) {
char[] buffer = new char[contentLength];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
int hasRead = 0;
int readsize = 0;
while (hasRead < contentLength) {
readsize = reader.read(buffer, hasRead, contentLength
- hasRead);
//
publishProgress((readsize * 100) / contentLength);
//
if (readsize == -1)
break;
hasRead += readsize;
}
// reader.read(buffer);
stream.close();
result = new String(buffer).toString().trim();
} else {
if (AppConstant.DEBUG_MODE)
Log.e(this.getClass().getName(), "Empty Result in WebService: "
+ serviceURL + "/" + methodName);
}
return result;
}
答案 0 :(得分:0)
您的API是否适合邮递员或提琴手的呼叫?
服务器端,您可以尝试:
public ActionResult<string> set(string test)
{
return Ok("Test is successfull");
}
或异步版本
public async Task<ActionResult<string>> set(string test)
{
return Ok("Test is successfull");
}
答案 1 :(得分:0)
使用改造而不是HttpClient。
添加到build.gradle
int main()
{
std::ifstream ifs("..\\tests\\data\\some_data.txt"); // Replace with your file
if(!ifs)
return -1;
std::unordered_map<std::string, std::array<double, 200>> dict;
dict.reserve(300000);
std::string line;
std::string key;
double weight;
std::size_t i;
while(getline(ifs, line))
{
std::istringstream record(line);
i = 0;
record >> key;
while(record >> weight)
{
dict[key].at(i++) = weight;
}
}
ifs.close();
// The whole file is loaded
return 0;
}
添加权限
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
使用在线工具从json响应中制作javaObject
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
用于改造的make接口:
http://www.jsonschema2pojo.org/
制作改造实例
public interface MyInterface {
@GET("posts")
Call<List<Post>> getPosts();
}
使用您的界面并进行改进,以生成简单的GET http请求
Retrofit retrofit = new Retrofit.Builder().baseUrl("YOUR_API")
.addConverterFactory(GsonConverterFactory.create())
.build();
然后
MyInterface myInterface = retrofit.create(MyInterface.class);
Call<List<Post>> listCall = MyInterface.getPosts();