現在、Webサービスを持つAndroidアプリケーションをビルドしています。 okhttp3を使用してSQLデータベースからデータを取得しようとしていますが、私は奇妙な応答を得ています。私はそれを理解できません。アンドロイド用のokhttpclientとLaravel API
public function getAccount(Request $request, User $user)
{
$email = $request->input('email');
//$response = Users::find($email);
$response = DB::table('users')->where('email', $email)->first();
$count = count($response);
if($count == 0) {
return Response::json([
'message' => 'An error occured',
], 401);
} else {
return Response::json([
'user' => $response->name,
'lastName' => $response->lastName,
'weight' => $response->weight,
'height' => $response->height,
'dob' => $response->DOB,
'email' => $response->email,
], 200);
}
そして、私のAndroidのコードは次のとおりです:Laravelでの私のAPIはあるresponse.body().toString()
として[email protected]
:
private void getDetails() {
Thread thread = new Thread(new Runnable(){
public void run() {
OkHttpClient client = new OkHttpClient();
// Json string with email and password
String bodyString = "{\n\t\"email\" : \"" + email +"\"\n}";
// Make HTTP POST request
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, bodyString);
Request request = new Request.Builder()
.url("http://192.168.1.100/CAB398/public/api/auth/getAccount")
.post(body)
.addHeader("accept", "application/json")
.addHeader("content-type", "application/json")
//.addHeader("cache-control", "no-cache")
//.addHeader("postman-token", "c3d60511-7e0f-5155-b5ad-66031ad76578")
.build();
// execute request
try {
Response response = client.newCall(request).execute();
String responseData = response.body().toString();
// Response code 200 means login details found in DB
if(response.code() == 200){
etfirstName.setText(responseData);
} else if(response.code() == 401){
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
//wait for thread to finish
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
問題は、私がの応答を取得していますということです。私は郵便配達を使用してAPI呼び出しをテストしているとの正しい応答を取得します:
{「ユーザー」:「ジョー」、「lastNameの」:「スミス」、「重量」:108、「高さ」を:179、 "dob": "1980-09-06"、 "email": "[email protected]"
私はリクエストをうんざりしていると思います。ビルダーが、私はこれを理解することはできません。
乾杯 ピーター
でいくつかの情報を見つけることができます。ああ、神様。私はそれが何か簡単でなければならないことを知っていた。ありがとうございます!!! – Schwartz914