// Main_Activity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView t1;
Button b1;
String one,key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1= (TextView) findViewById(R.id.textView);
b1= (Button) findViewById(R.id.but_click);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
peopleDetails();
}
public void peopleDetails()
{
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://echo.jsontest.com/key/value/one/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Apiservice service=retrofit.create(Apiservice.class);
Call<Modelclass> call=service.peopleDetails();
call.enqueue(new Callback<Modelclass>() {
@Override
public void onResponse(Call<Modelclass> call, Response<Modelclass> response) {
Toast.makeText(MainActivity.this, "onresponse", Toast.LENGTH_LONG).show();
key=response.body().getKey();
t1.setText(key.toString());
}
@Override
public void onFailure(Call<Modelclass> call, Throwable t) {
Toast.makeText(MainActivity.this, "onfail", Toast.LENGTH_LONG).show();
}
});
}
}
上面的代码抛出了nullpointerexeption。
我正在使用此网址请求:
http://echo.jsontest.com/key/value/one/two