改造空响应

时间:2017-12-02 04:05:43

标签: java android json retrofit okhttp

大家好,我和大学的小组决定创建一个应用程序,健身房的客户可以看到他/她的运动列表,我们正在使用改造来获得一个json。早期这段代码工作得非常好,但突然停止工作,现在我从回调中得到空响应和状态200。

image with the log.d 200 and null response

对不起我的英文。

我的JSON

{
"0":{
"id":"1",
"nome":"Fernanda Kondrat", 
"peso":"67.4",
"altura":"172",
"imc":"22.8",
"login":"fefe",
"senha":"1234"},
"lista_exercicios":[
{
"id":"1",
"id_aluno":"1",
"nome":"supino reto",
"num_serie":"3",
"num_repeticao":"10"
},
{
"id":"2",
"id_aluno":"1",
"nome":"agachamento hack",
"num_serie":"3",
"num_repeticao":"10"
},
{
"id":"3",
"id_aluno":"1",
"nome":"barra fixa",
"num_serie":"3",
"num_repeticao":"10"
 },
{
"id":"4",
"id_aluno":"1",
"nome":"leg press",
"num_serie":"4",
"num_repeticao":"10"
}
]
}

现在我的MainActivity.java

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Toast;

 import com.u9.fabapp.virtualgym.APIClient;
 import com.u9.fabapp.virtualgym.RespostaLogin;
 import com.u9.fabapp.virtualgym.Resposta;

 import retrofit.Callback;
 import retrofit.RetrofitError;
 import retrofit.client.Response;

public class MainActivity extends AppCompatActivity {

Button btn_login;
EditText edt_login;
EditText edt_senha;

private Callback<RespostaLogin> respostaCallback;

private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_login = findViewById(R.id.btn_login);
    edt_login = findViewById(R.id.edt_Login);
    edt_senha= findViewById(R.id.edt_Senha);

    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Log.d(TAG, "onClick: ");
            //Intent i = new Intent(MainActivity.this, ProfileActivity.class);
            //startActivity(i);

            String login = edt_login.getText().toString();
            String senha = edt_senha.getText().toString();
            if(TextUtils.isEmpty(login)|| TextUtils.isEmpty(senha)) {
                Toast.makeText(MainActivity.this, "Campos vazios", Toast.LENGTH_SHORT).show();
            } else {
                configurarCallback();
                //new APIClient().getRestService().setUsuarioLoginDTO(login, senha, respostaCallback);
                new APIClient().getRestService().setUsuarioLoginDTO(login, senha, respostaCallback);
            }

        }
    });

}

private void configurarCallback() {
    respostaCallback = new Callback<RespostaLogin>() {
        @Override
        public void success(RespostaLogin resposta, Response response) {

            Log.d(TAG, "Funcionou: "+ response.getStatus());
            Log.d(TAG, "Funcionou: " + resposta.getRETORNO());

            /*if (resposta.getRETORNO().equals("SUCESSO")){
                Intent intent1 = new Intent(MainActivity.this, ProfileActivity.class);
                startActivity(intent1);
            }else{
                Toast.makeText(MainActivity.this, resposta.getRETORNO() +" ,Verifique usuário e senha" , Toast.LENGTH_SHORT).show();
            }*/
        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(MainActivity.this, "Deu Ruim: " + error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    };
}

}

APIClient.java

import com.u9.fabapp.virtualgym.Resposta;

import com.u9.fabapp.virtualgym.RespostaLogin;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.http.GET;
import retrofit.http.Query;
import retrofit.http.Path;

public class APIClient {

private static RestAdapter REST_ADAPTER;

public APIClient(){
    createAdapterIfNeeded();
}

private static void createAdapterIfNeeded() {
    if(REST_ADAPTER == null){
        REST_ADAPTER = new RestAdapter
                .Builder()
                .setEndpoint("http://golfetto.16mb.com/virtual-fit/home/")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setClient(new OkClient())
                .build();
    }
}

public RestServices getRestService(){
    return REST_ADAPTER.create(RestServices.class);
}



public interface RestServices{

    @GET("/getExercicios.php")
    void setUsuarioLoginDTO(
            @Query("LOGIN") String login,
            @Query("SENHA") String senha,
            Callback<RespostaLogin> callbackResposta
    );
}

}

2 个答案:

答案 0 :(得分:1)

在“RestService”界面中,更改为:

  @GET("/getExercicios.php")

将其修改为:

  @GET("getExercicios.php")

删除“/”,因为它已被附加。

感谢和快乐的编码

答案 1 :(得分:1)

您服务的完整网址为

BASE_URL (defined at the time of createing retrofit object) + END_POINT_URL (defined in the interface)

您添加了&#34; http://golfetto.16mb.com/virtual-fit/home/&#34;作为基本URL,在界面中,您将端点定义为&#34; /getExercicios.php"所以完整的URL将是

"http://golfetto.16mb.com/virtual-fit/home//getExercicios.php"

这不是有效的网址。所以你必须删除&#39; /&#39;来自界面

界面将是

public interface RestServices{

@GET("getExercicios.php")
void setUsuarioLoginDTO(
        @Query("LOGIN") String login,
        @Query("SENHA") String senha,
        Callback<RespostaLogin> callbackResposta
);

}