The following is the CODE I am using to get the data from Codeforces.com API as JSON response. If anyone can please help me improve this to convert them to clickable links and then attach a web link to them. :
以下是我用来从Codeforces.com API获取数据作为JSON响应的CODE。如果有人可以帮我改进这个,将它们转换为可点击的链接,然后附上一个网页链接。 :
public class Http extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://codeforces.com/api/user.status?handle=";
String m = "";
public static String[] sarr = new String[200];
public static String[] name = new String[200];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView)findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("result");
}
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if(status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data);
//JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0).getJSONObject("problem");
JSONArray array = new JSONObject(data).getJSONArray("result");
String m1=null, m2=null, m3=null;
String n1 = System.getProperty("line.separator");
int cnt=0, flag=0;
for(int k=0; k<array.length(); k++)
{
last = array.getJSONObject(k).getJSONObject("problem");
JSONObject v = array.getJSONObject(k);
if(v.getString("verdict").contentEquals("OK")) {
m1 = last.getString("name");
m2= last.getString("contestId");
m2= m2.concat("/");
m3= last.getString("index");
m2 = m2.concat(m3);
flag = 0;
for(int i=0; i<cnt; i++) {
if (sarr[i].equals(m1)) {
flag = 1;
}
}
if(flag == 0) {
m = m.concat(m1);
m= m.concat(n1);
sarr[cnt] = m1;
name[cnt] = m2;
cnt++;
}
}
}
return last;
}
else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask <String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("avm12&from=1&count=100");
return m;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
}
}
}
1 个解决方案
#1
0
Make sure your main layout R.layout.httpex
has a ListView
.
确保您的主要布局R.layout.httpex具有ListView。
(Should look like this)
(应该看起来像这样)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Create a new layout list_item.xml
representing the data you want to display :
创建一个表示要显示的数据的新布局list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pTitle"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pUrl"
android:visibility="gone" />
</LinearLayout>
Then edit your code like this :
然后像这样编辑你的代码:
public class Http extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://codeforces.com/api/user.status?handle=";
String m = "";
ListView list;
public static ArrayList<HashMap<String, String>> problemdata= new ArrayList<HashMap<String, String>>();
//above ArrayList replacing sarr & name
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
list=(ListView)findViewById(R.id.list);
httpStuff = (TextView)findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("result");
}
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data);
//JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0).getJSONObject("problem");
JSONArray array = new JSONObject(data).getJSONArray("result");
String n1 = System.getProperty("line.separator");
for (int k = 0; k < array.length(); k++) {
last = array.getJSONObject(k).getJSONObject("problem");
JSONObject v = array.getJSONObject(k);
if (v.getString("verdict").contentEquals("OK")) {
HashMap<String, String> d = new HashMap<String, String>();
d.put("NAME",last.getString("name"));
d.put("URL","http://codeforces.com/problemset/problem/"+last.getString("contestId")+"/"+last.getString("index"));
if(!problemdata.contains(d)) {
m = m.concat(last.getString("name")).concat(n1); //m+=last.getString("name")+"\r\n";
problemdata.add(d);
}
}
}
return last;
} else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("avm12&from=1&count=100");
return m;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get the url from the invisible TextView ad opens it on the default browser
TextView t=(TextView)view.findViewById(R.id.pUrl);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(t.getText().toString())));
}
});
ListAdapter adapter = new SimpleAdapter(
Http.this, problemdata,
R.layout.list_item, new String[] { "NAME", "URL" }, new int[] { R.id.pTitle,
R.id.pUrl});
list.setAdapter(adapter);
}
}
}
#1
0
Make sure your main layout R.layout.httpex
has a ListView
.
确保您的主要布局R.layout.httpex具有ListView。
(Should look like this)
(应该看起来像这样)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Create a new layout list_item.xml
representing the data you want to display :
创建一个表示要显示的数据的新布局list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pTitle"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
android:padding="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pUrl"
android:visibility="gone" />
</LinearLayout>
Then edit your code like this :
然后像这样编辑你的代码:
public class Http extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://codeforces.com/api/user.status?handle=";
String m = "";
ListView list;
public static ArrayList<HashMap<String, String>> problemdata= new ArrayList<HashMap<String, String>>();
//above ArrayList replacing sarr & name
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
list=(ListView)findViewById(R.id.list);
httpStuff = (TextView)findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("result");
}
public JSONObject lastSub(String username) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
JSONObject last = new JSONObject(data);
//JSONObject last = new JSONObject(data).getJSONArray("result").getJSONObject(0).getJSONObject("problem");
JSONArray array = new JSONObject(data).getJSONArray("result");
String n1 = System.getProperty("line.separator");
for (int k = 0; k < array.length(); k++) {
last = array.getJSONObject(k).getJSONObject("problem");
JSONObject v = array.getJSONObject(k);
if (v.getString("verdict").contentEquals("OK")) {
HashMap<String, String> d = new HashMap<String, String>();
d.put("NAME",last.getString("name"));
d.put("URL","http://codeforces.com/problemset/problem/"+last.getString("contestId")+"/"+last.getString("index"));
if(!problemdata.contains(d)) {
m = m.concat(last.getString("name")).concat(n1); //m+=last.getString("name")+"\r\n";
problemdata.add(d);
}
}
}
return last;
} else {
Toast.makeText(Http.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = lastSub("avm12&from=1&count=100");
return m;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get the url from the invisible TextView ad opens it on the default browser
TextView t=(TextView)view.findViewById(R.id.pUrl);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(t.getText().toString())));
}
});
ListAdapter adapter = new SimpleAdapter(
Http.this, problemdata,
R.layout.list_item, new String[] { "NAME", "URL" }, new int[] { R.id.pTitle,
R.id.pUrl});
list.setAdapter(adapter);
}
}
}