当前位置: 首页 > 知识库问答 >
问题:

获取HTML页面源代码而不是JSON Reponse

包和泰
2023-03-14
        HttpPost httppost=new HttpPost(url);
        httppost.setHeader("Authorization", "Basic "+Base64.encodeToString("abc:xyz".getBytes(), Base64.NO_WRAP));

         //Posting request on htt
          httppost.setHeader( "Content-Type", "application/json" );        
          httppost.setHeader("Accept","application/json");
            JSONObject jsonPara = new JSONObject();     
            jsonPara.put("password", "abc");
            jsonPara.put("username", "abc");




            JSONObject jsonobj=new JSONObject();

            jsonobj.put("request", "syncdata/get_country");
            jsonobj.put("para",jsonPara);

            Log.i("jason Object", jsonobj.toString());

            StringEntity se2 = new StringEntity(jsonobj.toString());

            se2.setContentEncoding("UTF-8");
            se2.setContentType("application/json");

            httppost.setEntity(se2);  
        HttpResponse httpResponse=httpClient.execute(httppost);
        HttpEntity httpEntity=httpResponse.getEntity();
        is=httpEntity.getContent();
    }
    catch(UnsupportedEncodingException ae)
    {
    ae.printStackTrace();           
    }
    catch(ClientProtocolException ae)
    {
        ae.printStackTrace();
    }
    catch(IOException ae)
    {
        ae.printStackTrace();
    }
    catch(JSONException e)
    {
        e.printStackTrace();
    }

    try
    {
        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();
        json =sb.toString();
    }
    catch(Exception ae)
    {
        ae.printStackTrace();
    }

    try
    {
        jObj=new JSONObject(json);
    }
    catch(JSONException e)
    {
    e.printStackTrace();    
    }

    return jObj;
}

共有1个答案

宰父嘉胜
2023-03-14

参考这个代码。

希望对你有帮助...

MainActivity.java

    public class MainActivity extends Activity {

InputStream is=null;
String result=null;
String line=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/text.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
}    

try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("Pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}    

try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
final String[] str1 = new String[JA.length()];       
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
str1[i]=json.getString("name");
}

final AutoCompleteTextView text = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
final List<String> list = new ArrayList<String>();

for(int i=0;i<str1.length;i++)
{
list.add(str1[i]);
}

Collections.sort(list);

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
text.setThreshold(1);
text.setAdapter(dataAdapter);

text.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), list.get(arg2).toString(), Toast.LENGTH_SHORT).show();
}
});

}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
<?php
$host='127.0.0.1';
$uname='root';
$pwd='password';
$db='android';
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$r=mysql_query("select * from class",$con);
while($row=mysql_fetch_array($r))
{
$cls[]=$row;
}
print(json_encode($cls));
mysql_close($con);
?>
 类似资料:
  • 我有一个非常简单的问题。我试图从linkedIn页面的html中获取工作描述,但是我没有得到页面的html,而是得到了几行看起来像javascript代码的代码。我对这一点很陌生,所以任何帮助都将不胜感激!谢谢 这是我的密码: 当我运行这个时,我没有得到我期望包含工作描述的html。。。我只得到几行javascript代码。

  • 问题内容: 这个IFRAME的标准代码,是否有一种方法可以将src URL替换为Just html代码?所以我的问题很简单,我有一个页面,它加载了来自MySQL的HTML主体,我想在框架中显示该代码,以便使其独立于页面的其余部分,并不受特定边界的限制。 问题答案: 您可以使用数据URL进行此操作。这将整个文档包含在单个HTML字符串中。例如,以下HTML: 可以这样编码: 然后设置为iframe

  • <?php $http = HttpRequest::newSession(); $response = $http->get('http://www.baidu.com/'); $content = $response->body(); // 网页源码

  • 问题内容: 如何不使用来获取html源代码? 我需要知道这一点,因为在某些虚拟主机上已禁用,因此您不能使用。是否可以使用cURL获取html文件的源(如果启用了cURL支持)?如果是这样,怎么办?谢谢。 问题答案: 请尝试以下操作: 我只推荐小文件使用。大文件将作为一个整体读取,并且可能会产生内存错误。 编辑:在评论中进行了一些讨论之后,我们发现问题是服务器无法解析主机名,并且该页面另外是http

  • 问题内容: JavaScript代码将从www.example.com通过google chrome中的网址栏启动,因此我无法使用jquery。我的目标是当我在www.example.com中启动代码时,将www.example.com/page.html的完整html源代码传递给javascript中的变量。这可能吗?如果可以,怎么办?我知道要获取当前页面源,但我不确定如何做到这一点。我认为可以

  • 我想使用页面而不是使用页面。但是当使用html页面时,我得到了错误。但是如果我使用jsp页面,我没有得到任何错误。 当我使用