Ada*_*dam 28 post android retrofit
我尝试使用Retrofit库在Android上使用API时失败,但在使用POSTMAN时我可以看到预期的结果.
邮递员设定
api url(基地+控制器)
HTTP方法设置为POST
点击了from-data或x-www-form-urlencoded
然后我在键/值字段上传递两个参数.
ANDROID改装设置
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Query("code") String code,
@Query("monthact") String monthact,
Callback<LandingPageReport> cb);
@FormUrlEncoded
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
@Field("monthact") String monthact,
Callback<LandingPageReport> cb);
Run Code Online (Sandbox Code Playgroud)
这些选项都不起作用.但结果是{}.
UPDATE
使用标准HttpClient(和HttpPost)类的相同设置工作正常.
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("code", "testcode"));
urlParameters.add(new BasicNameValuePair("monthact", "feb-2015"));
post.setEntity(new UrlEncodedFormEntity(urlParameters));
HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)
为什么我不能做这个请求并在Retrofit中获得正确的响应?
更新2
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Query("code") String code,
@Query("monthact") String monthact,
Callback<List<LandingPageReport>> cb);
@FormUrlEncoded
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
@Field("monthact") String monthact,
Callback<List<LandingPageReport>>> cb);
Run Code Online (Sandbox Code Playgroud)
玩完之后我觉得我找到了问题的根源.我已经更新了我的改装代码以便接收List<LandingPageReport>.但现在发生这种错误
retrofit.RetrofitError:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但在第1行第2行路径$ BEGIN_OBJECT $
原因是我消耗了2个api(webapi和wcf).我所有的其他json响应都是对象数组.[{},{}]但是在这个电话中我收到了这个
{
"GetDetailWithMonthWithCodeResult": [
{
"code": "test",
"field1": "test",
}
]
}
Run Code Online (Sandbox Code Playgroud)
但我仍然无法解析响应.
Kes*_*era 31
的build.gradle
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'// compulsory
compile 'com.squareup.retrofit2:converter-gson:2.1.0' //for retrofit conversion
Run Code Online (Sandbox Code Playgroud)
Bel*_*han 23
这是一个简单的解决方案,我们不需要使用JSON
public interface RegisterAPI {
@FormUrlEncoded
@POST("/RetrofitExample/insert.php")
public void insertUser(
@Field("name") String name,
@Field("username") String username,
@Field("password") String password,
@Field("email") String email,
Callback<Response> callback);
}
Run Code Online (Sandbox Code Playgroud)
发送数据的方法
private void insertUser(){
//Here we will handle the http request to insert user to mysql db
//Creating a RestAdapter
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(ROOT_URL) //Setting the Root URL
.build(); //Finally building the adapter
//Creating object for our interface
RegisterAPI api = adapter.create(RegisterAPI.class);
//Defining the method insertuser of our interface
api.insertUser(
//Passing the values by getting it from editTexts
editTextName.getText().toString(),
editTextUsername.getText().toString(),
editTextPassword.getText().toString(),
editTextEmail.getText().toString(),
//Creating an anonymous callback
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
//On success we will read the server's output using bufferedreader
//Creating a bufferedreader object
BufferedReader reader = null;
//An string to store output from the server
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
//Reading the output in the string
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//Displaying the output as a toast
Toast.makeText(MainActivity.this, output, Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
//If any error occured displaying the error as toast
Toast.makeText(MainActivity.this, error.toString(),Toast.LENGTH_LONG).show();
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用php aur获取任何其他服务器端脚本的发布请求.
Ada*_*dam 15
我找到了解决方案.这个问题在我的课程结构中是一个问题.所以我像下面的样本一样更新它们.
public class LandingPageReport {
private ArrayList<LandingPageReportItem> GetDetailWithMonthWithCodeResult;
// + Getter Setter methods
}
public class LandingPageReportItem {
private String code;
private String field1;
// + Getter Setter methods
}
Run Code Online (Sandbox Code Playgroud)
然后我使用这种改造配置
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
@Field("monthact") String monthact,
Callback<LandingPageReport> cb);
Run Code Online (Sandbox Code Playgroud)
小智 6
您应该为此创建一个界面,就像它运行良好一样
public interface Service {
@FormUrlEncoded
@POST("v1/EmergencyRequirement.php/?op=addPatient")
Call<Result> addPerson(@Field("BloodGroup") String bloodgroup,
@Field("Address") String Address,
@Field("City") String city, @Field("ContactNumber") String contactnumber,
@Field("PatientName") String name,
@Field("Time") String Time, @Field("DonatedBy") String donar);
}
Run Code Online (Sandbox Code Playgroud)
或者您可以访问http://teachmeandroidhub.blogspot.com/2018/08/post-data-using-retrofit-in-android.html
您可以访问https://github.com/rajkumu12/GetandPostUsingRatrofit
| 归档时间: |
|
| 查看次数: |
108313 次 |
| 最近记录: |