开始学习 java 和 android 开发,但我仍然在将 JSON 数据从 URL 显示到 textview 时遇到一些困难。基本上我要做的是当用户在查找字段中键入并单击查找按钮时,数据将显示他们在文本视图中键入的特定项目。任何建议或建议都会有所帮助。谢谢你。
这是我的主要活动:
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.alexanderherrera.data.JsonDataClass;
import com.alexanderherrera.data.WebConnectClass;
import com.alexanderherrera.elements.AppElements;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
Context context;
LinearLayout baseLinearLayout;
LinearLayout.LayoutParams baseLayoutParams;
TextView mainTextView;
EditText userInputText;
Button findButton;
String urlApiJsonString;
static TextView urlAPIJsonTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
baseLinearLayout = AppElements.getBaseLayout(context);
mainTextView = AppElements.getMainTextView(context);
urlAPIJsonTextView = AppElements.getJsonTextView(context);
if (WebConnectClass.getConnectionStatus(context)) {//If true.
getUrlApiData apiData = new getUrlApiData();
apiData.execute(JsonDataClass.urlApiStringLink);
//urlApiJsonString = JsonDataClass.onPostExecute(JsonDataClass.urlApiStringLink);
} else {
urlAPIJsonTextView.setText("There was No Connection!");
}
userInputText = AppElements.getUserInputText(context);
userInputText.setHint("Enter App Name or Genre Type");
findButton = AppElements.getFindEditTextButton(context);
findButton.setText("Find");
findButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Get the text inputed by the user:
String userInput = userInputText.getText().toString();
}
});
baseLinearLayout.addView(mainTextView);
baseLinearLayout.addView(userInputText);
baseLinearLayout.addView(findButton);
baseLinearLayout.addView(urlAPIJsonTextView);
setContentView(baseLinearLayout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
static class getUrlApiData extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String urlApiResponseString = "";
try {
URL urlApi = new URL(JsonDataClass.urlApiStringLink);
urlApiResponseString = JsonDataClass.getUrlApiResponse(urlApi);
} catch (MalformedURLException e) {
urlApiResponseString = "Another ERROR has occurred!";
Log.e(WebConnectClass.TAG,"ERROR!!!", e);
}
return urlApiResponseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String jsonResult = "";
String artistNameString = "";
String isGameCenterEnabledString = "";
String priceString = "";
String versionString = "";
String primaryGenreNameString = "";
String averageUserRatingString = "";
try {
@SuppressWarnings("static-access")
JSONArray iosAppJSONArray = new JSONArray(result.valueOf("results"));
int i;
for (i = 0; i < iosAppJSONArray.length(); i++) {
JSONObject iosAppJSONObject = iosAppJSONArray.getJSONObject(i);
artistNameString = iosAppJSONObject.getString("artistName");
isGameCenterEnabledString = iosAppJSONObject.getString("isGameCenterEnabled");
priceString = iosAppJSONObject.getString("price");
versionString = iosAppJSONObject.getString("version");
primaryGenreNameString = iosAppJSONObject.getString("primaryGenreName");
averageUserRatingString = iosAppJSONObject.getString("averageUserRating");
jsonResult = "Artist Name: " + artistNameString;
urlAPIJsonTextView.setText(jsonResult);
}
} catch (JSONException e) {
}
}
}
}
这个类是我创建文本视图、按钮和编辑文本的地方:
package com.alexanderherrera.elements;
import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class AppElements {
public static LinearLayout getBaseLayout(Context context) {
// Initialize Linear Layout One:
LinearLayout baseLayout = new LinearLayout(context);
baseLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams baseParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
baseLayout.setLayoutParams(baseParams);
return baseLayout;
}
public static TextView getMainTextView(Context context) {
TextView mainTextView = new TextView(context);
LayoutParams baseParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mainTextView.setLayoutParams(baseParams);
mainTextView.setText("Browse through the selection of IOS games in the appstore.");
mainTextView.setTextSize(24.0f);
return mainTextView;
}
public static TextView getJsonTextView(Context context) {
TextView jsonTextView = new TextView(context);
LayoutParams baseParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
jsonTextView.setLayoutParams(baseParams);
jsonTextView.setTextSize(12.0f);
return jsonTextView;
}
public static EditText getUserInputText(Context context) {
EditText userInputText = new EditText(context);
LayoutParams baseParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
userInputText.setLayoutParams(baseParams);
return userInputText;
}
public static Button getFindEditTextButton(Context context) {
Button findButton = new Button(context);
LayoutParams baseParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
findButton.setLayoutParams(baseParams);
return findButton;
}
}
JSON数据类:
package com.alexanderherrera.data;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.alexanderherrera.iosgamesinfo.MainActivity;
import android.util.Log;
public class JsonDataClass {
public static String urlApiStringLink = "http://itunes.apple.com/search?term=app&term=toppaid&entity=software";
private final String artistNameString;
private final String isGameCenterEnabledString;
private final String priceString;
private final String versionString;
private final String primaryGenreNameString;
private final String averageUserRating;
private JsonDataClass(String artistNameString, String isGameCenterEnabledString, String priceString, String versionString, String primaryGenreNameString, String averageUserRating) {
this.artistNameString = artistNameString;
this.isGameCenterEnabledString = isGameCenterEnabledString;
this.priceString = priceString;
this.versionString = versionString;
this.primaryGenreNameString = primaryGenreNameString;
this.averageUserRating = averageUserRating;
}
public String setArtistNameString() {
return artistNameString;
}
public String setIsGameCenterEnabledString() {
return isGameCenterEnabledString;
}
public String setPriceString() {
return priceString;
}
public String setVersionString() {
return versionString;
}
public String setPrimaryGenreNameString() {
return primaryGenreNameString;
}
public String setAverageUserRating() {
return averageUserRating;
}
public static String getUrlApiResponse(URL iosAppUrl) {
String urlApiResponse = "";
try {
URLConnection iosAppURLConnection = iosAppUrl.openConnection();
BufferedInputStream iosAppBuffInputStream = new BufferedInputStream(iosAppURLConnection.getInputStream());
byte[] iosAppBytes = new byte[1024];
int bytesRead = 0;
StringBuffer responseBuff = new StringBuffer();//Use StringBuffer to append.
while ((bytesRead = iosAppBuffInputStream.read(iosAppBytes)) != -1) {
urlApiResponse = new String(iosAppBytes, 0, bytesRead);
responseBuff.append(urlApiResponse);
}
urlApiResponse = responseBuff.toString();
Log.i(WebConnectClass.TAG, urlApiResponse);
} catch (Exception e) {
urlApiResponse = "An ERROR has occurred!!";
Log.e(WebConnectClass.TAG, "There was an ERROR with the URL API Response!");
}
return urlApiResponse;
}
}