我試圖在我的應用程序上實現一個搜索選項,從聯機數據庫獲取數據。我在我的libs文件夾中有okhhtp3.jar和okio.jar。每當我將文本輸入到搜索字段時,應用程序就會崩潰並關閉。這是我的代碼搜索:
`在Android中搜索將無法正常工作;應用程序崩潰
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.SearchView;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
`public class Search extends Fragment {
private SearchView searchField;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.search, container, false);
this.searchField = (SearchView) rootView.findViewById(R.id.searchView);
// perform set on query text listener event
this.searchField.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
String url = "https://api.themoviedb.org/3/search/movie?api_key&language=en-US&query="+query+"&page=1&include_adult=false";
//URLUtil.isValidUrl(query);
if (URLUtil.isValidUrl(url) == true) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, "{}");
Request request = new Request.Builder().url(url).get().build();
try {
Response response = client.newCall(request).execute();
//System.out.println(response.message());
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// do something when text changes
return false;
}
});
this.searchField.getQuery();
return rootView;
}
}
例外我越來越:
04-15 08:33:18 E/InputEventSender: Exception dispatching finished signal.
04-15 08:33:18 E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
04-15 08:33:18 E/MessageQueue-JNI: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at okhttp3.Dns$1.lookup(Dns.java:39)
at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:170)
at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:136)
at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:81)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:171)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
at okhttp3.RealCall.execute(RealCall.java:63) at Search$1.onQueryTextSubmit(Search.java:46) at android.widget.SearchView.onSubmitQuery(SearchView.java:1285) at android.widget.SearchView.-wrap8(SearchView.java) at android.widget.SearchView$6.onEditorAction(SearchView.java:1262) at android.widget.TextView.doKeyDown(TextView.java:6193) at android.widget.TextView.onKeyDown(TextView.java:6075) at android.widget.AutoCompleteTextView.onKeyDown(AutoCompleteTextView.java:829) at android.view.KeyEvent.dispatch(KeyEvent.java:2688) at android.view.View.dispatchKeyEvent(View.java:9960) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.support.v4.view.ViewPager.dispatchKeyEvent(ViewPager.java:2761) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1630) at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:405) at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1798) at android.app.Activity.dispatchKeyEvent(Activity.java:3021) at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:547) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59) at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:319) at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
at com.a
04-15 08:33:18 D/AndroidRuntime: Shutting down VM
04-15 08:33:18 E/AndroidRuntime: FATAL EXCEPTION: main
Process: PID: 4675
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74) at java.net.InetAddress.getAllByName(InetAddress.java:752) at okhttp3.Dns$1.lookup(Dns.java:39)
我會很感激任何幫助!
將stacktrace添加到您的問題。你可以在Logcat中找到堆棧跟蹤。請參閱[this](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this)以供參考 – Zoe