Я пытаюсь отобразить элементы из блога в своем приложении, поэтому мне удалось создать JSON, и я вижу свой ответ.
Мне удалось отобразить элементы JSONObject в ListView.
Но проблема сейчас в том, что я хочу отобразить другой элемент JSONObject в WebView после нажатия onListItemClick и Logcat говорит, что для моего KEY не найдено значение url.
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
try {
JSONArray jsonPosts = mData.getJSONArray("users");
JSONObject jsonPost = jsonPosts.getJSONObject(position);
String blogUrl = jsonPost.getString("url");
Intent intent = new Intent(getActivity(), SeedsWebView.class);
intent.setData(Uri.parse(blogUrl));
startActivity(intent);
} catch (JSONException e) {
Log.e(TAG, msg, e);
}
}
SeedsWebView.class
public class SeedsWebView extends Activity {
protected String mUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seeds_web_view);
Intent intent = getIntent();
Uri blogUri = intent.getData();
mUri = blogUri.toString();
WebView web = (WebView)findViewById(R.id.webView);
web.loadUrl(mUri);
}
}
getArticle.php
<?php
$conn = mysql_connect('localhost','root','');
$db = mysql_select_db('seeds',$conn);
$sql = mysql_query("SELECT * FROM users") or die (mysql_error());
$result = mysql_num_rows($sql);
$json = array();
if($result > 0 ){
while($row=mysql_fetch_assoc($sql)){
$json['users'][]=$row;
}
$json["success"] = 1;
echo json_encode($json);
} else {
$json["success"] = 0;
$json["message"] = "No products found";
echo json_encode($json);
}
mysql_close($conn);
?>
Logcat
10-01 07:55:13.490: E/HomeFragment(877): Exception caught
10-01 07:55:13.490: E/HomeFragment(877): org.json.JSONException: No value for url
10-01 07:55:13.490: E/HomeFragment(877): at org.json.JSONObject.get(JSONObject.java:354)
10-01 07:55:13.490: E/HomeFragment(877): at org.json.JSONObject.getString(JSONObject.java:510)
10-01 07:55:13.490: E/HomeFragment(877): at com.codecamp14.seeds.HomeFragment.onListItemClick(HomeFragment.java:71)
10-01 07:55:13.490: E/HomeFragment(877): at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
10-01 07:55:13.490: E/HomeFragment(877): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-01 07:55:13.490: E/HomeFragment(877): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-01 07:55:13.490: E/HomeFragment(877): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-01 07:55:13.490: E/HomeFragment(877): at android.widget.AbsListView$1.run(AbsListView.java:3423)
10-01 07:55:13.490: E/HomeFragment(877): at android.os.Handler.handleCallback(Handler.java:725)
10-01 07:55:13.490: E/HomeFragment(877): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 07:55:13.490: E/HomeFragment(877): at android.os.Looper.loop(Looper.java:137)
10-01 07:55:13.490: E/HomeFragment(877): at android.app.ActivityThread.main(ActivityThread.java:5039)
10-01 07:55:13.490: E/HomeFragment(877): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 07:55:13.490: E/HomeFragment(877): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 07:55:13.490: E/HomeFragment(877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-01 07:55:13.490: E/HomeFragment(877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-01 07:55:13.490: E/HomeFragment(877): at dalvik.system.NativeStart.main(Native Method)
Выход JSON
{
users: [
{
id: "8",
geTitle: "Thank you Jesus",
getDesc: null,
getAmount: "12",
getDays: "23",
getArticle: "udhfujfhadjfdjfd",
time: "0000-00-00 00:00:00"},
{
id: "10",
geTitle: "I love Jesus",
getDesc: null,
getAmount: "12",
getDays: "23",
getArticle: "Thank you Jesus...",
time: "0000-00-00 00:00:00"},
{
id: "18",
geTitle: "Gabe's Senior Painting Thesis",
getDesc: null,
getAmount: "5600000",
getDays: "23",
getArticle: "As a Studio Art major at Wesleyan, I am about to begin work on my senior thesis in painting. This experience, which I have looked forward to with excitement and anxiety since my freshman year, will culminate in an exhibition of my work in Wesleyan's Zilkha Gallery in April 2015. For my thesis I plan to infuse my painting practice with the interests I have acquired through my studies in sculpture, design, performance, and American studies at Wes and beyond over the past three years. While Wesleyan generously supplies its students with studio space, the amount of supplies I will need to purchase to complete a year's worth of work is daunting. By working as economically as possible, and putting money from campus jobs toward funding my process, I hope to be able to affordably create and complete my best and most ambitious artworks possible. All of your donations will go directly to art supplies (paint, canvas, gesso, brushes, etc...I am working to complile a detailed budget, though it will surely be changing as I go). I appreciate any amount of support toward my thesis--be it monetary, or simply coming out to see my and my fellow majors' work in the Spring. I promise to find a way to dutifuly pay tribute and thanks to all who chose to donate. Thank you so much!",
time: "0000-00-00 00:00:00"},
{
id: "19",
geTitle: "I want to become a musician, but idont have money.Please help",
getDesc: null,
getAmount: "300000000",
getDays: "12",
getArticle: "",
time: "0000-00-00 00:00:00"},
{
id: "17",
geTitle: "Help Andy march RCC Indoor!",
getDesc: "Help Andy march RCC Indoor! Since 2010, all I wanted to ever do was be apart of the RCC Indoor Percussion Ensemble.",
getAmount: "4000000",
getDays: "3",
getArticle: "Since 2010, all I wanted to ever do was be apart of the RCC Indoor Percussion Ensemble. Over a span of four years, I've worked hard, practiced day and night, took lessons, and did whatever it took to be apart of that group. This past summer, I marched with the 16-time champion Blue Devils and had the best summer of my life, learning and performing in front of thousands of people every night! With your help, I can make my dream come true. Currently, I am a student at University of California, Riverside. Aside from going to school, I am teaching music to help pay for school, housing, and indoor. If any of you can help donate to my cause, I would greatly appreciate it. I am also offering lessons to anyone interested in learning how to play marimba! If you have any questions, feel free to send me a message! :) Thank you! http://www.youtube.com/watch?v=FEXbP4agoMs",
time: "0000-00-00 00:00:00"}
],
success: 1
}
Задача ещё не решена.
Других решений пока нет …