在Android Studio中无法parsing符号HttpGet,HttpClient,HttpResponce
我只是复制Http
所有jar文件,但Android Studio
不能导入所有这些jar文件。它给出了一个错误: Cannot resolve symbol HttpGet,HttpClient,HttpResponse
。
我的Activity
文件在这里: –
public class MainActivity extends AppCompatActivity { ArrayList<Product> productslist; ProductAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); productslist = new ArrayList<Product>(); new JSONAsyncTask().execute("http://opencart.codeniques.com/myshop/?route=feed/web_api/products&id=60&key=test123"); ListView listView = (ListView)findViewById(R.id.list); adapter = new ProductAdapter(getApplicationContext(),R.layout.row,productslist); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(),productslist.get(position).getName(),Toast.LENGTH_LONG).show(); } }); } class JSONAsyncTask extends AsyncTask<String,Void,Boolean>{ ProgressDialog dialog; @Override protected void onPreExecute() { super.onPreExecute(); dialog = new ProgressDialog(MainActivity.this); dialog.setMessage("Loading, please wait"); dialog.setTitle("Connecting server"); dialog.show(); dialog.setCancelable(false); } @Override protected Boolean doInBackground(String... params) { try{ HttpGet httppost = new HttpGet(params[0]); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); if(status == 200){ HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity); JSONObject jsono = new JSONObject(data); JSONArray jarray = jsono.getJSONArray("products"); for (int i = 0; i < jarray.length(); i++) { JSONObject object = jarray.getJSONObject(i); Product actor = new Product(); actor.setId(object.getString("id")); actor.setName(object.getString("name")); actor.setDescription(object.getString("description")); actor.setHref(object.getString("href")); actor.setPrice(object.getString("pirce")); actor.setImage(object.getString("thumb")); actor.setSpecial(object.getString("special")); actor.setRating(object.getString("rating")); productslist.add(actor); } return true; } }catch (JSONException e){ Log.e("Error :",e.getMessage()); }catch (ParseException e){ Log.e("Error :",e.getMessage()); }catch (IOException e){ Log.e("Error :",e.getMessage()); }catch (Exception e){ Log.e("Error :",e.getMessage()); } return false; } @Override protected void onPostExecute(Boolean aBoolean) { dialog.cancel(); adapter.notifyDataSetChanged(); if(aBoolean == false){ Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show(); } } }}
在这里我的gradle
在这里:
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "22.0.1" defaultConfig { applicationId "android.catalyst.com.newjsonarray" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }}} dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1'}
只需在你的依赖中添加这个
compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5'
最后
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5' }
编辑
你可以加
android { useLibrary 'org.apache.http.legacy' }
我希望它可以帮助你。
你不得不使用URLConnection或者降级到sdk 22(编译'com.android.support:appcompat-v7:22.2.0')
如果你需要sdk 23,把它添加到你的gradle中:
在依赖中添加:
compile 'org.apache.httpcomponents:httpcore:4.4.1' compile 'org.apache.httpcomponents:httpclient:4.5'
并添加这个
android { useLibrary 'org.apache.http.legacy' }
HttpClient在API级别22中被弃用,在API级别23中被删除。您必须使用URLConnection
。
请从libs文件夹中删除所有的Http jar文件,并在gradle文件中添加下面的依赖关系:
compile 'org.apache.httpcomponents:httpclient:4.5' compile 'org.apache.httpcomponents:httpcore:4.4.3'
谢谢。
请在您的关系上添加这些代码。 它会工作。
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile 'com.android.support:design:23.1.0' compile 'com.android.support:cardview-v7:23.1.0' compile 'com.android.support:recyclerview-v7:23.1.0' compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' }
谷歌正式向开发者build议使用Volley库进行networking相关的工作。 在这里 ,所以它的时间切换到Volley,Happy编码
你刚刚重build你的项目
compile fileTree(dir: 'libs', include: ['*.jar'])
这只是在SDK 23中,Httpclient和其他人已被弃用。 您可以通过更改目标SDK版本来更正它,如下所示:
apply plugin: 'com.android.application' android { compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig { applicationId "vahid.hoseini.com.testclient" minSdkVersion 10 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' }
对我来说,下面的帮助
查找Android/Sdk/platforms/android-23/optional,
org.apache.http.legacy.jar
Android/Sdk/platforms/android-23/optional,
将其添加到您的依赖项中。
资源
请从'libs'文件夹中删除所有的Http jar
文件,并在gradle文件中添加下面的依赖关系:
compile 'org.apache.httpcomponents:httpclient:4.5' compile 'org.apache.httpcomponents:httpcore:4.4.3'
要么
useLibrary 'org.apache.http.legacy'
- 里面的OnClickListener我不能访问很多东西 – 如何处理?
- ListView:与LinkMovementMethod的TextView使列表项不可点击?
- 在android活动之间传递string数组
- java.lang.NoClassDefFoundError:android中的javax.activation.DataHandler
- 在Firebase侦听器中设置Singleton属性值
- Android游戏开发SDK?
- Google Cloud Messaging – 在networking状态发生变化之前有时收不到消息
- 在Android中使用VideoView播放video
- 如何以编程方式清除应用程序数据