请勿将此复制标记为已尝试此question
中给出的解决方案我想从 restcountries api
中获取数据MainActivity
public class MainActivity extends AppCompatActivity {
TextView tv4,tv1,tv2,tv3;
EditText edit;
public void loadCountryInfo(View view) {
//EditText cn=(EditText) findViewById(R.id.CountryName);
edit=(EditText)findViewById(R.id.ed1);
final String countryName=edit.getText().toString();
final String strURL="http://restcountries.eu";
if (countryName.length() == 0) {
Toast.makeText(this, "No Country Specified", Toast.LENGTH_SHORT).show();
} else {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(strURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofiApi service = retrofit.create(RetrofiApi.class);
Call<CountriesInfo> call = service.getCountryInfo(countryName);//countryName
call.enqueue(new Callback<CountriesInfo>() {
@Override
public void onResponse(Call<CountriesInfo> call, Response<CountriesInfo> response) {
tv4=(TextView) findViewById(R.id.textView4);
tv1=(TextView) findViewById(R.id.textView1);
tv2=(TextView) findViewById(R.id.textView2);
tv3=(TextView) findViewById(R.id.textView3);
try {
tv1.setText(response.body().getCapital());
tv2.setText(response.body().getArea().toString());
tv3.setText(response.body().getPopulation().toString());
tv4.setText(response.body().getName().toString());
} catch (Exception e) {
tv1.setText(countryName);
Toast.makeText(MainActivity.this, "Something Went Wrong! Maybe due to invalid country name or Something like this.", Toast.LENGTH_SHORT).show();
int code =response.code();
System.out.println("this is errror♠♠ "+code);
e.printStackTrace();
}
}
@Override
public void onFailure(Call<CountriesInfo> call, Throwable t) {
//System.out.println("Here is some this is errror♠♠ ");
String code = t.getMessage();
System.out.println("this is errror♠♠ helloooooooooo "+code);
Toast.makeText(MainActivity.this, "Something Went Wrong! Maybe due to invalid country name.", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button) findViewById(R.id.button);
//loadCountryInfo();
}
}
CountriesInfo :getters和setter
public class CountriesInfo {
@SerializedName("name")
@Expose
private String name;
@SerializedName("capital")
@Expose
private String capital;
@SerializedName("altSpellings")
@Expose
private List<String> altSpellings = new ArrayList<String>();
@SerializedName("relevance")
@Expose
private String relevance;
@SerializedName("region")
@Expose
private String region;
@SerializedName("subregion")
@Expose
private String subregion;
// @SerializedName("translations")
// @Expose
// private Translations translations;
@SerializedName("population")
@Expose
private Integer population;
@SerializedName("latlng")
@Expose
private List<Double> latlng = new ArrayList<Double>();
@SerializedName("demonym")
@Expose
private String demonym;
@SerializedName("area")
@Expose
private Double area;
@SerializedName("gini")
@Expose
private Double gini;
@SerializedName("timezones")
@Expose
private List<String> timezones = new ArrayList<String>();
@SerializedName("borders")
@Expose
private List<String> borders = new ArrayList<String>();
@SerializedName("nativeName")
@Expose
private String nativeName;
@SerializedName("callingCodes")
@Expose
private List<String> callingCodes = new ArrayList<String>();
@SerializedName("topLevelDomain")
@Expose
private List<String> topLevelDomain = new ArrayList<String>();
@SerializedName("alpha2Code")
@Expose
private String alpha2Code;
@SerializedName("alpha3Code")
@Expose
private String alpha3Code;
@SerializedName("currencies")
@Expose
private List<String> currencies = new ArrayList<String>();
@SerializedName("languages")
@Expose
private List<String> languages = new ArrayList<String>();
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The capital
*/
public String getCapital() {
return capital;
}
/**
*
* @param capital
* The capital
*/
public void setCapital(String capital) {
this.capital = capital;
}
/**
*
* @return
* The altSpellings
*/
public List<String> getAltSpellings() {
return altSpellings;
}
/**
*
* @param altSpellings
* The altSpellings
*/
public void setAltSpellings(List<String> altSpellings) {
this.altSpellings = altSpellings;
}
/**
*
* @return
* The relevance
*/
public String getRelevance() {
return relevance;
}
/**
*
* @param relevance
* The relevance
*/
public void setRelevance(String relevance) {
this.relevance = relevance;
}
/**
*
* @return
* The region
*/
public String getRegion() {
return region;
}
/**
*
* @param region
* The region
*/
public void setRegion(String region) {
this.region = region;
}
/**
*
* @return
* The subregion
*/
public String getSubregion() {
return subregion;
}
/**
*
* @param subregion
* The subregion
*/
public void setSubregion(String subregion) {
this.subregion = subregion;
}
/**
*
* @return
* The translations
*/
// public Translations getTranslations() {
// return translations;
// }
/**
*
* @param translations
* The translations
*/
// public void setTranslations(Translations translations) {
// this.translations = translations;
// }
/**
*
* @return
* The population
*/
public Integer getPopulation() {
return population;
}
/**
*
* @param population
* The population
*/
public void setPopulation(Integer population) {
this.population = population;
}
/**
*
* @return
* The latlng
*/
public List<Double> getLatlng() {
return latlng;
}
/**
*
* @param latlng
* The latlng
*/
public void setLatlng(List<Double> latlng) {
this.latlng = latlng;
}
/**
*
* @return
* The demonym
*/
public String getDemonym() {
return demonym;
}
/**
*
* @param demonym
* The demonym
*/
public void setDemonym(String demonym) {
this.demonym = demonym;
}
/**
*
* @return
* The area
*/
public Double getArea() {
return area;
}
/**
*
* @param area
* The area
*/
public void setArea(Double area) {
this.area = area;
}
/**
*
* @return
* The gini
*/
public Double getGini() {
return gini;
}
/**
*
* @param gini
* The gini
*/
public void setGini(Double gini) {
this.gini = gini;
}
/**
*
* @return
* The timezones
*/
public List<String> getTimezones() {
return timezones;
}
/**
*
* @param timezones
* The timezones
*/
public void setTimezones(List<String> timezones) {
this.timezones = timezones;
}
/**
*
* @return
* The borders
*/
public List<String> getBorders() {
return borders;
}
/**
*
* @param borders
* The borders
*/
public void setBorders(List<String> borders) {
this.borders = borders;
}
/**
*
* @return
* The nativeName
*/
public String getNativeName() {
return nativeName;
}
/**
*
* @param nativeName
* The nativeName
*/
public void setNativeName(String nativeName) {
this.nativeName = nativeName;
}
/**
*
* @return
* The callingCodes
*/
public List<String> getCallingCodes() {
return callingCodes;
}
/**
*
* @param callingCodes
* The callingCodes
*/
public void setCallingCodes(List<String> callingCodes) {
this.callingCodes = callingCodes;
}
/**
*
* @return
* The topLevelDomain
*/
public List<String> getTopLevelDomain() {
return topLevelDomain;
}
/**
*
* @param topLevelDomain
* The topLevelDomain
*/
public void setTopLevelDomain(List<String> topLevelDomain) {
this.topLevelDomain = topLevelDomain;
}
/**
*
* @return
* The alpha2Code
*/
public String getAlpha2Code() {
return alpha2Code;
}
/**
*
* @param alpha2Code
* The alpha2Code
*/
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}
/**
*
* @return
* The alpha3Code
*/
public String getAlpha3Code() {
return alpha3Code;
}
/**
*
* @param alpha3Code
* The alpha3Code
*/
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}
/**
*
* @return
* The currencies
*/
public List<String> getCurrencies() {
return currencies;
}
/**
*
* @param currencies
* The currencies
*/
public void setCurrencies(List<String> currencies) {
this.currencies = currencies;
}
/**
*
* @return
* The languages
*/
public List<String> getLanguages() {
return languages;
}
/**
*
* @param languages
* The languages
*/
public void setLanguages(List<String> languages) {
this.languages = languages;
}
}
RetrofiApi界面:HTTP请求
public interface RetrofiApi {
// String base_url = "https://restcountries.eu/";
@GET("/rest/v2/name/")
Call<CountriesInfo> getCountryInfo(@Query("q") String name);
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sony.finalsubmission.MainActivity">
<EditText
android:id="@+id/ed1"
android:layout_width="322dp"
android:layout_height="87dp"
android:hint="kombro"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView1"
android:layout_width="322dp"
android:layout_height="50dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="322dp"
android:layout_height="50dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView3"
android:layout_width="322dp"
android:layout_height="50dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView4"
android:layout_width="322dp"
android:layout_height="50dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ed1"
android:onClick="loadCountryInfo"/>
</android.support.constraint.ConstraintLayout>
我在 com.example.sony.finalsubmission.MainActivity $ 1.onResponse(MainActivity.java:53)
中获得 java.lang.NullPointerExceptionlogcat的
11-27 13:38:35.965 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 669: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
11-27 13:38:36.005 3476-3476/com.example.sony.finalsubmission E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
11-27 13:38:36.005 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve instanceof 193 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16777: Landroid/widget/TextView;.getAutoSizeMaxTextSize ()I
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16778: Landroid/widget/TextView;.getAutoSizeMinTextSize ()I
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16779: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16780: Landroid/widget/TextView;.getAutoSizeTextAvailableSizes ()[I
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16781: Landroid/widget/TextView;.getAutoSizeTextType ()I
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16824: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithConfiguration (IIII)V
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16825: Landroid/widget/TextView;.setAutoSizeTextTypeUniformWithPresetSizes ([II)V
11-27 13:38:36.015 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16826: Landroid/widget/TextView;.setAutoSizeTextTypeWithDefaults (I)V
11-27 13:38:36.025 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16779: Landroid/widget/TextView;.getAutoSizeStepGranularity ()I
11-27 13:38:36.025 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve static method 15266: Landroid/text/StaticLayout$Builder;.obtain (Ljava/lang/CharSequence;IILandroid/text/TextPaint;I)Landroid/text/StaticLayout$Builder;
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16336: Landroid/widget/Button;.getAutoSizeMaxTextSize ()I
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16337: Landroid/widget/Button;.getAutoSizeMinTextSize ()I
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16338: Landroid/widget/Button;.getAutoSizeStepGranularity ()I
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16339: Landroid/widget/Button;.getAutoSizeTextAvailableSizes ()[I
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16340: Landroid/widget/Button;.getAutoSizeTextType ()I
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16346: Landroid/widget/Button;.setAutoSizeTextTypeUniformWithConfiguration (IIII)V
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16347: Landroid/widget/Button;.setAutoSizeTextTypeUniformWithPresetSizes ([II)V
11-27 13:38:36.045 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16348: Landroid/widget/Button;.setAutoSizeTextTypeWithDefaults (I)V
11-27 13:38:36.825 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/graphics/drawable/Icon;)
11-27 13:38:36.825 3476-3476/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve virtual method 16516: Landroid/widget/ImageView;.setImageIcon (Landroid/graphics/drawable/Icon;)V
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve static method 20043: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
11-27 13:38:43.005 3476-3494/com.example.sony.finalsubmission W/dalvikvm: VFY: unable to resolve static method 20042: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
11-27 13:38:43.335 3476-3476/com.example.sony.finalsubmission W/System.err: java.lang.NullPointerException
11-27 13:38:43.335 3476-3476/com.example.sony.finalsubmission W/System.err: at com.example.sony.finalsubmission.MainActivity$1.onResponse(MainActivity.java:54)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at android.os.Looper.loop(Looper.java:136)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-27 13:38:43.345 3476-3476/com.example.sony.finalsubmission W/System.err: at dalvik.system.NativeStart.main(Native Method)
有人可以帮我这个吗?
答案 0 :(得分:0)
调用.toSring()或null响应体上的任何方法,就像您在下面所做的那样,将导致NullPointerException
tv2.setText(response.body().getArea().toString());
tv3.setText(response.body().getPopulation().toString());
tv4.setText(response.body().getName().toString());
你的Base URl根据Retrofit看起来应该是这样的。请注意,它以/
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
你的Api宣言应该是这样的。请注意,它不以/
@GET("users/list")