展会信息港展会大全

android json 应用,data retrive from a url
来源:互联网   发布日期:2016-01-13 21:58:08   浏览:1986次  

导读:[代码]JsonActivity.java001//JsonActivity.java002packagecom.v3;003004importjava.io.BufferedReader;005importjava.io.IOException;006importjava.io.InputStream;007importjava.io.InputStreamReader;008imp......

[代码] JsonActivity.java

001

//JsonActivity.java

002

package com.v3;

003

004

import java.io.BufferedReader;

005

import java.io.IOException;

006

import java.io.InputStream;

007

import java.io.InputStreamReader;

008

import java.io.Reader;

009

import java.net.URI;

010

import java.util.ArrayList;

011

import java.util.List;

012

import java.util.StringTokenizer;

013

014

import org.apache.http.HttpEntity;

015

import org.apache.http.HttpResponse;

016

import org.apache.http.HttpStatus;

017

import org.apache.http.client.HttpClient;

018

import org.apache.http.client.methods.HttpGet;

019

import org.apache.http.impl.client.DefaultHttpClient;

020

import org.apache.http.params.CoreProtocolPNames;

021

import org.json.JSONArray;

022

import org.json.JSONObject;

023

import org.json.JSONStringer;

024

025

import android.app.Activity;

026

import android.os.Bundle;

027

import android.os.Message;

028

import android.util.Log;

029

import android.view.View;

030

import android.widget.Button;

031

import android.widget.TextView;

032

033

public class JsonActivity extends Activity {

034

Button button,buttonClear;

035

TextView textView;

036

String page;

037

String str="";

038

039

String url = "http://192.168.1.118/androidjsontest/index.php";

040

/** Called when the activity is first created. */

041

@Override

042

public void onCreate(Bundle savedInstanceState) {

043

super.onCreate(savedInstanceState);

044

setContentView(R.layout.main);

045

button = (Button) findViewById(R.id.btnFetch);

046

buttonClear = (Button) findViewById(R.id.btnClear);

047

textView = (TextView) findViewById(R.id.txtView);

048

button.setOnClickListener(new Button.OnClickListener()

049

{

050

public void onClick(View v)

051

{

052

examineJSONFile();

053

}

054

});

055

buttonClear.setOnClickListener(new Button.OnClickListener()

056

{

057

public void onClick(View v)

058

{

059

textView.setText("");

060

}

061

});

062

063

}

064

065

066

public String executeHttpGet(String URL) throws Exception

067

{

068

//This method for HttpConnection

069

BufferedReader bufferedReader = null;

070

try

071

{

072

HttpClient client = new DefaultHttpClient();

073

client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");

074

HttpGet request = new HttpGet();

075

request.setHeader("Content-Type", "text/plain; charset=utf-8");

076

request.setURI(new URI(URL));

077

HttpResponse response = client.execute(request);

078

bufferedReader = new BufferedReader(newInputStreamReader(response.getEntity().getContent()));

079

080

StringBuffer stringBuffer = new StringBuffer("");

081

String line = "";

082

083

String NL = System.getProperty("line.separator");

084

while ((line = bufferedReader.readLine()) != null)

085

{

086

stringBuffer.append(line + NL);

087

System.out.print(stringBuffer);

088

}

089

bufferedReader.close();

090

page = stringBuffer.toString();

091

System.out.println(page+"page");

092

return page;

093

}

094

finally

095

{

096

if (bufferedReader != null)

097

{

098

try

099

{

100

bufferedReader.close();

101

}

102

catch (IOException e)

103

{

104

Log.d("BBB", e.toString());

105

}

106

}

107

}

108

}

109

110

111

void examineJSONFile()

112

{

113

try

114

{

115

116

String Value="" ;

117

String str = "";

118

page = executeHttpGet(url);

119

Log.i("hello", str.toString());

120

121

JSONObject object=new JSONObject(page);

122

str=str+object.names().toString();

123

124

String sub1=str.substring(2, str.length()-2);

125

String[] names=sub1.split("\",\"");

126

127

for (int i=0;i<names.length;i++) {

128

Value=Value+names[i] +":"+object.get(names[i])+"\n";

129

System.out.println(names[i]);

130

131

}

132

133

/* String sub=str.substring(1, str.length()-1);

134

String[] names=sub.split(",");

135

String[] keyValue=new String[names.length];

136

137

for (int i=0;i<names.length;i++) {

138

names[i]=names[i].replace("\"","");

139

Value=Value+names[i] +":"+object.get(names[i])+"\n";

140

//keyValue[i]=names[i] +":"+object.get(names[i])+"\n";

141

System.out.println(names[i]);

142

143

}*/

144

145

textView.setText(Value);

146

147

/*for(int i=0;i<keyValue.length;i++){

148

textView.setText(keyValue[i]);

149

}*/

150

151

Log.i("hello", str.toString());

152

153

}

154

catch (Exception je)

155

{

156

textView.setText("Error w/file: " + je.getMessage());

157

}

158

}

159

160

}

[代码] main.xml

01

//main.xml

02

//==================

03

<?xml version="1.0" encoding="utf-8"?>

04

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

05

android:orientation="vertical"

06

android:background="#acd654"

07

android:layout_width="fill_parent"

08

android:layout_height="fill_parent"

09

>

10

<Button android:text="Fetch_Data"

11

android:id="@+id/btnFetch"

12

android:layout_width="match_parent"

13

android:layout_height="wrap_content"></Button>

14

<Button android:text="Clear_Screen"

15

android:id="@+id/btnClear"

16

android:layout_width="wrap_content"

17

android:layout_height="wrap_content"></Button>

18

<TextView android:text=""

19

android:gravity="center"

20

android:textColor="#000a44"

21

android:id="@+id/txtView"

22

android:layout_width="fill_parent"

23

android:layout_height="fill_parent"/>

24

</LinearLayout>

[代码] android menifest.xml

01

//android menifest.xml

02

//=========================

03

?xml version="1.0" encoding="utf-8"?>

04

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

05

package="com.v3"

06

android:versionCode="1"

07

android:versionName="1.0">

08

<uses-sdk android:minSdkVersion="8" />

09

10

<application android:icon="@drawable/icon" android:label="@string/app_name">

11

<activity android:name=".JsonActivity"

12

android:label="@string/app_name">

13

<intent-filter>

14

<action android:name="android.intent.action.MAIN" />

15

<category android:name="android.intent.category.LAUNCHER" />

16

</intent-filter>

17

</activity>

18

19

</application>

20

<uses-permission android:name="android.permission.INTERNET"/>

21

</manifest>

赞助本站

人工智能实验室

相关热词: json 应用

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港