加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

android通过http请求获得json内容

发布时间:2020-12-15 03:20:18 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 //JsonActivity.javapackage com.v3;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputSt

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

//JsonActivity.java
package com.v3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class JsonActivity extends Activity {
Button button,buttonClear;
TextView textView;
String page;
String str="";

String url = "http://192.168.1.118/androidjsontest/index.php";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.btnFetch);
buttonClear = (Button) findViewById(R.id.btnClear);
textView = (TextView) findViewById(R.id.txtView);
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
examineJSONFile();
}
});
buttonClear.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
textView.setText("");
}
});

}

public String executeHttpGet(String URL) throws Exception
{
//This method for HttpConnection
BufferedReader bufferedReader = null;
try
{
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,"android");
HttpGet request = new HttpGet();
request.setHeader("Content-Type","text/plain; charset=utf-8");
request.setURI(new URI(URL));
HttpResponse response = client.execute(request);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuffer stringBuffer = new StringBuffer("");
String line = "";

String NL = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line + NL);
System.out.print(stringBuffer);
}
bufferedReader.close();
page = stringBuffer.toString();
System.out.println(page+"page");
return page;
}
finally
{
if (bufferedReader != null)
{
try
{
bufferedReader.close();
}
catch (IOException e)
{
Log.d("BBB",e.toString());
}
}
}
}

void examineJSONFile()
{
try
{

String Value="" ;
String str = "";
page = executeHttpGet(url);
Log.i("hello",str.toString());

JSONObject object=new JSONObject(page);
str=str+object.names().toString();

String sub1=str.substring(2,str.length()-2);
String[] names=sub1.split("","");

for (int i=0;i<names.length;i++) {
Value=Value+names[i] +":"+object.get(names[i])+"n";
System.out.println(names[i]);

}

/* String sub=str.substring(1,str.length()-1);
  String[] names=sub.split(",");
  String[] keyValue=new String[names.length];

  for (int i=0;i<names.length;i++) {
  names[i]=names[i].replace(""","");
  Value=Value+names[i] +":"+object.get(names[i])+"n";
  //keyValue[i]=names[i] +":"+object.get(names[i])+"n";
System.out.println(names[i]);

}*/

textView.setText(Value);

/*for(int i=0;i<keyValue.length;i++){
  textView.setText(keyValue[i]);
  }*/

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

}
catch (Exception je)
{
textView.setText("Error w/file: " + je.getMessage());
}
}

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读