Java Android:在edittext中插入空格会导致崩溃
我的编辑文本用作搜索框,我从烂番茄API获取电影,使用编辑文本中的文本,问题是。当一个空格被插入应用程序崩溃时,我假设我需要将空格转换成+,但我不知道如何添加此代码或如何添加,我希望这里的人能够帮助我 这是我的代码:Java Android:在edittext中插入空格会导致崩溃,java,android,json,url,uri,Java,Android,Json,Url,Uri,我的编辑文本用作搜索框,我从烂番茄API获取电影,使用编辑文本中的文本,问题是。当一个空格被插入应用程序崩溃时,我假设我需要将空格转换成+,但我不知道如何添加此代码或如何添加,我希望这里的人能够帮助我 这是我的代码: private TextView searchBox; private Button bGo, bCancelAddFromWeb; private ListView moviesList; public final static int ACTIV
private TextView searchBox;
private Button bGo, bCancelAddFromWeb;
private ListView moviesList;
public final static int ACTIVITY_WEB_ADD = 3;
public List<String> movieTitles;
public List<String> movieSynopsis;
public List<String> movieImgUrl;
private ProgressDialog pDialog;
// the Rotten Tomatoes API key
private static final String API_KEY = "8q6wh77s65a54w433cab9rbsq";
// the number of movies to show
private static final int MOVIE_PAGE_LIMIT = 8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_add_from_web);
InitializeVariables();
}
/*
* Initializing the variables and creating the bridge between the views from
* the xml file and this class
*/
private void InitializeVariables() {
searchBox = (EditText) findViewById(R.id.etSearchBox);
bGo = (Button) findViewById(R.id.bGo);
bCancelAddFromWeb = (Button) findViewById(R.id.bCancelAddFromWeb);
moviesList = (ListView) findViewById(R.id.list_movies);
bGo.setOnClickListener(this);
bCancelAddFromWeb.setOnClickListener(this);
moviesList.setOnItemClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bGo:
new RequestTask()
.execute("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey="
+ API_KEY
+ "&q="
+ searchBox.getText()
+ "&page_limit=" + MOVIE_PAGE_LIMIT);
break;
case R.id.bCancelAddFromWeb:
finish();
break;
}
}
private void refreshMoviesList(List<String> movieTitles) {
moviesList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, movieTitles
.toArray(new String[movieTitles.size()])));
}
private class RequestTask extends AsyncTask<String, String, String> {
// make a request to the specified url
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
// make a HTTP request
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// close connection
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (Exception e) {
Log.d("Test", "Couldn't make a successful request!");
}
return responseString;
}
// if the request above completed successfully, this method will
// automatically run so you can do something with the response
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MovieAddFromWeb.this);
pDialog.setMessage("Searching...");
pDialog.show();
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
try {
// convert the String response to a JSON object
JSONObject jsonResponse = new JSONObject(response);
// fetch the array of movies in the response
JSONArray jArray = jsonResponse.getJSONArray("movies");
// add each movie's title to a list
movieTitles = new ArrayList<String>();
// newly added
movieSynopsis = new ArrayList<String>();
movieImgUrl = new ArrayList<String>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject movie = jArray.getJSONObject(i);
movieTitles.add(movie.getString("title"));
movieSynopsis.add(movie.getString("synopsis"));
movieImgUrl.add(movie.getJSONObject("posters").getString(
"profile"));
}
// refresh the ListView
refreshMoviesList(movieTitles);
} catch (JSONException e) {
Log.d("Test", "Couldn't successfully parse the JSON response!");
}
pDialog.dismiss();
}
}
@Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
Intent openMovieEditor = new Intent(this, MovieEditor.class);
openMovieEditor.putExtra("movieTitle", movieTitles.get(position));
// newly added
openMovieEditor.putExtra("movieSynopsis", movieSynopsis.get(position));
openMovieEditor.putExtra("movieImgUrl", movieImgUrl.get(position));
openMovieEditor.putExtra("callingActivity", ACTIVITY_WEB_ADD);
startActivityForResult(openMovieEditor, ACTIVITY_WEB_ADD);
}
}
私有文本视图搜索框;
私人按钮bGo,B取消来自Web的按钮;
私有列表查看电影列表;
公共最终静态int活动\网络\添加=3;
公开名单电影;
公共列表电影综合征;
公开名单电影大师;
私人对话;
//烂番茄API密钥
私有静态最终字符串API_KEY=“8q6wh77s65a54w433cab9rbsq”;
//要放映的电影数量
私有静态最终整版电影页面限制=8;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.movie\u从web添加);
初始化变量();
}
/*
*初始化变量并在来自的视图之间创建桥接
*xml文件和这个类
*/
私有无效初始化变量(){
searchBox=(EditText)findViewById(R.id.etSearchBox);
bGo=(按钮)findviewbyd(R.id.bGo);
bCanceladFromWeb=(按钮)findViewById(R.id.bCanceladFromWeb);
moviesList=(ListView)findViewById(R.id.list\u movies);
bGo.setOnClickListener(此);
bCanceladFromWeb.setOnClickListener(此);
moviesList.setOnItemClickListener(this);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.bGo:
新请求任务()
.执行(”http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey="
+API_密钥
+“&q=”
+searchBox.getText()
+“&page_limit=“+MOVIE_page_limit”);
打破
案例R.id.B从Web取消:
完成();
打破
}
}
私有电影列表(电影列表){
moviesList.setAdapter(新阵列适配器)(此,
android.R.layout.simple\u list\u item\u 1,电影
.toArray(新字符串[movieTitles.size()]);
}
私有类RequestTask扩展了AsyncTask{
//向指定的url发出请求
@凌驾
受保护的字符串doInBackground(字符串…uri){
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse响应;
字符串responseString=null;
试一试{
//发出HTTP请求
response=httpclient.execute(新的HttpGet(uri[0]);
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString=out.toString();
}否则{
//密切联系
response.getEntity().getContent().close();
抛出新IOException(statusLine.getReasonPhrase());
}
}捕获(例外e){
Log.d(“测试”,“无法发出成功的请求!”);
}
回报率;
}
//如果上述请求成功完成,则此方法将
//自动运行,以便您可以对响应执行某些操作
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MovieAddFromWeb.this);
设置消息(“搜索…”);
pDialog.show();
}
@凌驾
受保护的void onPostExecute(字符串响应){
super.onPostExecute(响应);
试一试{
//将字符串响应转换为JSON对象
JSONObject jsonResponse=新的JSONObject(响应);
//获取响应中的电影数组
JSONArray jArray=jsonResponse.getJSONArray(“电影”);
//将每部电影的标题添加到列表中
movieTitles=新的ArrayList();
//新增
movieSynopsis=newarraylist();
movieImgUrl=newarraylist();
for(int i=0;i
这是包含错误的日志:
01-14 20:19:19.591: D/Test(907): Couldn't make a successful request!
01-14 20:19:19.690: D/AndroidRuntime(907): Shutting down VM
01-14 20:19:19.700: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-14 20:19:19.801: E/AndroidRuntime(907): FATAL EXCEPTION: main
01-14 20:19:19.801: E/AndroidRuntime(907): java.lang.NullPointerException
01-14 20:19:19.801: E/AndroidRuntime(907): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-14 20:19:19.801: E/AndroidRuntime(907): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-14 20:19:19.801: E/AndroidRuntime(907): at org.json.JSONObject.<init>(JSONObject.java:154)
01-14 20:19:19.801: E/AndroidRuntime(907): at org.json.JSONObject.<init>(JSONObject.java:171)
01-14 20:19:19.801: E/AndroidRuntime(907): at il.jb.projectpart2.MovieAddFromWeb$RequestTask.onPostExecute(MovieAddFromWeb.java:152)
01-14 20:19:19.801: E/AndroidRuntime(907): at il.jb.projectpart2.MovieAddFromWeb$RequestTask.onPostExecute(MovieAddFromWeb.java:1)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:99)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:137)
01-14 20:19:19.801: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-14 20:19:19.801: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
01-14 20:19:19.801: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:511)
01-14 20:19:19.801: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-14 20:19:19.801: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-14 20:19:19.801: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)
01-14 20:19:19.591:D/Test(907):无法成功发出请求!
String search = searchBox.getText();
search = search.replace(" ", "+");
case R.id.bGo:
new RequestTask()
.execute("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey="
+ API_KEY
+ "&q="
+ URLEncoder.encode(searchBox.getText(), "UTF-8")
+ "&page_limit=" + MOVIE_PAGE_LIMIT);