public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.clcik);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List list = new ArrayList<>();
String url ="http://abcd.abcd.com/api/SearchJob? limit=5&jobkeyword=oil&countrytext=United%20Kingdom&location=London%20[Greater%2 0London]&apikey=1111111111&siteid=1";
HttpPost httppost = new HttpPost(url);
try{
HttpClient client = new DefaultHttpClient() ;
HttpResponse response = client.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String json = sb.toString();
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = jsonObject.getJSONArray("jobslist");
for(int i=0;i<jsonArray.length();i++){
JSONObject c = jsonArray.getJSONObject(i);
String jobtitle = c.getString("jobtitle");
list.add(jobtitle);
Log.d("list", jobtitle);
}
ArrayAdapter<List> adapter = new ArrayAdapter<List>(getApplicationContext(),android.R.layout.simple_list_item_1,list);
ListView listView = (ListView) findViewById(R.id.jsonlist);
listView.setAdapter(adapter);
}catch(Exception e){
}
}
});
}
}
這是我的MainActivity代碼。我試圖從Web服務中獲取數據,但我無法顯示它。從webservice使用android獲取Json
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/clcik"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/jsonlist"
></ListView>
</LinearLayout>
這是我的xml代碼。
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.practice.android.json"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
這是我的清單文件。可以有人請幫幫我。忘記它的URL只是一個例子。我的代碼有問題嗎?我使用sdk 23和Http方法是decripted,但我看到人們說,它仍然有效。
[Android應用程序中的Json解析]可能的重複(http://stackoverflow.com/questions/3819273/json-parsing-in-android-application) – camelCaseCoder
所以有什麼問題?你有錯誤嗎?最糟糕的事情就是使用空的'catch'像這樣'catch(Exception e){}'add'e.printStacktrace();'看看發生了什麼 – Yazan
不,我不會得到任何錯誤 –