无法解析符号HTTPGET,HttpClient的,Htt的presponce Android Studio中 [英] Cannot resolve symbol HttpGet,HttpClient,HttpResponce in Android Studio
本文介绍了无法解析符号HTTPGET,HttpClient的,Htt的presponce Android Studio中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是复制 HTTP
的所有jar文件,但 Android的工作室
水湿导入所有这些jar文件。它给出了一个错误:无法解析符号HTTPGET,HttpClient的,Htt的presponse
。
我的活动
文件是在这里: -
公共类MainActivity扩展AppCompatActivity {ArrayList的<产品与GT;产品列表;
ProductAdapter适配器;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
产品列表=新的ArrayList<产品及GT;();
新JSONAsyncTask().execute(\"http://opencart.$c$cniques.com/myshop/?route=feed/web_api/products&id=60&key=test123\"); ListView控件的ListView =(ListView控件)findViewById(R.id.list);
适配器=新ProductAdapter(getApplicationContext(),R.layout.row,产品列表); listView.setAdapter(适配器); listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
Toast.makeText(getApplicationContext(),productslist.get(position).getName(),Toast.LENGTH_LONG).show();
}
});
}类JSONAsyncTask扩展的AsyncTask<弦乐,太虚,布尔> { ProgressDialog对话框; @覆盖
在preExecute保护无效(){
super.on preExecute();
对话框=新ProgressDialog(MainActivity.this);
dialog.setMessage(加载中,请稍候);
dialog.setTitle(连接服务器);
dialog.show();
dialog.setCancelable(假);
} @覆盖
保护布尔doInBackground(字符串... PARAMS){
尝试{
HTTPGET httppost =新HTTPGET(PARAMS [0]);
HttpClient的HttpClient的=新DefaultHttpClient();
HTT presponse响应= httpclient.execute(httppost); 。INT状态= response.getStatusLine()的getStatus code(); 如果(状态== 200){
HttpEntity实体= response.getEntity();
字符串数据= EntityUtils.toString(实体); JSONObject的jsono =新的JSONObject(数据);
JSONArray jarray = jsono.getJSONArray(产品); 的for(int i = 0; I< jarray.length();我++){
的JSONObject对象= jarray.getJSONObject(I) 产品演员=新产品(); actor.setId(object.getString(ID));
actor.setName(object.getString(名字));
actor.setDescription(object.getString(说明));
actor.setHref(object.getString(的href));
actor.setPrice(object.getString(pirce));
actor.setImage(object.getString(拇指));
actor.setSpecial(object.getString(特));
actor.setRating(object.getString(等级)); productslist.add(演员);
}
返回true;
}
}赶上(JSONException E){
Log.e(错误,e.getMessage());
}赶上(ParseException的E){
Log.e(错误,e.getMessage());
}赶上(IOException异常五){
Log.e(错误,e.getMessage());
}赶上(例外五){
Log.e(错误,e.getMessage());
}
返回false;
}
@覆盖
保护无效onPostExecute(布尔aBoolean){
dialog.cancel();
adapter.notifyDataSetChanged();
如果(aBoolean == FALSE){
Toast.makeText(getApplicationContext(),无法读取从服务器上的数据,Toast.LENGTH_LONG).show();
}
}
}}
和我在这里的 gradle这个
是在这里: -
应用插件:'com.android.application
安卓{
compileSdkVersion 23
buildToolsVersion22.0.1defaultConfig {
的applicationIDandroid.catalyst.com.newjsonarray
16的minSdkVersion
targetSdkVersion 23
版本code 1
的versionName1.0
}
buildTypes {
发布 {
minifyEnabled假
proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard-rules.pro
}}}
依赖{
编译文件树(导演:'库',包括:['的* .jar'])testCompile'的JUnit:JUnit的:4.12
编译com.android.support:appcompat-v7:23.0.1
编译com.android.support:design:23.0.1'}
解决方案
就在您的相关性添加此
编译org.apache.httpcomponents:的HttpCore:4.4.1
编译org.apache.httpcomponents:HttpClient的:4.5'
最后
{相关性
编译文件树(导演:'库',包括:['的* .jar'])testCompile'的JUnit:JUnit的:4.12
编译com.android.support:appcompat-v7:23.0.1
编译com.android.support:design:23.0.1
编译org.apache.httpcomponents:的HttpCore:4.4.1
编译org.apache.httpcomponents:HttpClient的:4.5'
}
我希望它会帮助你。
I just copy all the jar files of Http
but Android Studio
cann't import all these jar files.It gives an error :
Cannot resolve symbol HttpGet,HttpClient,HttpResponse
.
My Activity
file is here:-
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();
}
}
}}
and here my gradle
is here:-
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'}
解决方案
Just add this in your dependencies
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
Finally
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'
}
I hope it will helps you .
这篇关于无法解析符号HTTPGET,HttpClient的,Htt的presponce Android Studio中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文