【发布时间】:2019-10-03 09:52:54
【问题描述】:
我正在使用 ngrok 将我的本地 php 项目放到网络中。我需要在登录操作中获取用户的 ID。我的安卓给了我
响应{protocol=http/1.1, code=404, message=Not Found, url=http://ab873755.ngrok.io/site/login?login=123&pass=123456}
在改造 Android Studio 中无法获得响应。网址是正确的。在 Postman 中检查过
当我将此网址放入 Postman 时,一切都很好,并给出了正确的响应。 本页返回
{"user_id":1}
package com.example.backgroundservice.ui.login;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class WResponce {
@SerializedName("user_id")
@Expose
private String user_id;
public String getUserId() {
return user_id;
}
public void setUserId(String user_id) {
this.user_id = user_id;
}
}
package com.example.backgroundservice.ui.login;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface JSONPlaceHolderApi {
@GET("site/login")
public Call<WResponce> getResponce(
@Query("login") String login,
@Query("pass") String password
);
}
package com.example.backgroundservice.ui.login;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NetworkClient {
public static final String BASE_URL = "http://ab873755.ngrok.io/";
public static Retrofit retrofit;
public static Retrofit getRetrofitClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
package com.example.backgroundservice.ui.login;
import android.net.http.HttpResponseCache;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.backgroundservice.R;
import com.example.backgroundservice.ReceiveData;
import java.io.IOException;
public class LoginActivity extends AppCompatActivity {
final Handler mHandler = new Handler();
private Thread mUiThread;
private JSONPlaceHolderApi jsonPlaceHolderApi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void login(View view) throws IOException, InterruptedException{
final EditText username = findViewById(R.id.username);
final EditText password = findViewById(R.id.password);
Retrofit retrofit = NetworkClient.getRetrofitClient();
jsonPlaceHolderApi = retrofit.create(JSONPlaceHolderApi.class);
Call call = jsonPlaceHolderApi.getResponce(username.getText().toString(),password.getText().toString());
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
WResponce res = (WResponce) response.body();
String a = ((WResponce) response.body()).getUserId();
Log.d("myres",a);
Log.d("myres",response.body().toString());
}
Log.d("mymsg",response.message());
Log.d("mymsg",response.toString());
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
}
【问题讨论】:
-
api url 是真的吗?如果是这样,那么它不起作用。从不同的设备检查尝试。
-
重启你的 ngrok 隧道并获得一个新的 URL。 ngrok 隧道免费版即将到期。
-
我一直无法连接到无线网络。所以我经常重新启动 ngrok 并获取新的网址。所以你将无法检查我的 api。我只是希望有人能在我的代码中看到一些错误,或者可能面临同样的问题。我一直在考虑问题是否出在我的后端。就像我只返回像 $str="user_id:".$model->id; 这样的字符串($model->id 是我数据库中的一个 id。我正在使用 Yii2 框架)并且我的程序需要某种类型的 json 对象并且只找到 text/html。还尝试返回 json_encode(array("user_id"=>$model->id));好像没用。