将Java代码移植到C#

时间:2019-03-09 09:55:46

标签: java c# porting

我在Java中有这段代码,我正尝试将其移植到C#中,我已经移植了大部分代码,但是我真的不明白如何使这两行在C#中工作。我认为这是关于如何编写Java代码的问题,因为我已经在Java中嵌套了两个方法,并且据我了解,在C#中执行此方法有些棘手。

如果有人可以帮助我,我将非常高兴。

对不起,我的英语。

Java代码:

package com.example.jsontablewebapi2;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity
{
    private Context context;
    private GridView gridView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        gridView = (GridView) findViewById(R.id.products);
        downloadJSON("http://192.168.1.11/api/api.php");
        //downloadJSON("http://192.168.1.11:49958");
    }
    private void createGridView(String json) throws JSONException
    {
        JSONArray jsonArray = new JSONArray(json);
        GridAdapter productAdapter = new GridAdapter( this, jsonArray);
        gridView.setAdapter( productAdapter );
    }
    //I think the problems starts here
    private void downloadJSON(final String urlWebService)
    {
        class DownloadJSON extends AsyncTask<Void, Void, String>
        {
            @Override
            protected void onPreExecute()
            {
                super.onPreExecute();
            }
            @Override
            protected void onPostExecute(String s)
            {
                super.onPostExecute(s);
                try
                {
                    createGridView(s);
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }
            @Override
            protected String doInBackground(Void... voids)
            {
                try {
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String json;
                    while ((json = bufferedReader.readLine()) != null)
                    {
                        sb.append(json + "\n");
                    }
                    return sb.toString().trim();
                } catch (Exception e)
                {
                    return null;
                }
            }
        }
        DownloadJSON downloadJSON = new DownloadJSON();
        downloadJSON.execute();
    }
}

C#代码:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;
using Java.IO;
using Java.Net;
using Org.Json;
using System;
using System.Text;

namespace AdrianAndroid
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private Context context;
        private GridView gridView;
        protected void onCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
            context = this;
            gridView = (GridView)FindViewById(Resource.Id.products);
            downloadJSON("http://192.168.1.11:49958");
        }

        public void createGridView(string json)
        {
            JSONArray jsonArray = new JSONArray(json);
            GridAdapter productAdapter = new GridAdapter(this, jsonArray);
            gridView.SetAdapter(productAdapter);
        }

        public class DownloadJSON : AsyncTask<string, string, string>
        {

            protected override void OnPreExecute()
            {
                base.OnPreExecute();
            }
            protected override void OnPostExecute(string s)
            {
                base.OnPostExecute(s);
                try
                {
                    //Error CS0120 on this line
                    createGridView(s);
                }
                catch (JSONException e)
                {
                    e.PrintStackTrace();
                }
            }
            protected override string RunInBackground(params string[] @params)
            {
                try
                {
                    //Error CS0103 on this line (I think because it doesn't know where to get urlWebService 
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection)url.OpenConnection();
                    StringBuilder sb = new StringBuilder();
                    //Fix1
                    InputStreamReader inputStream = new InputStreamReader(con.InputStream);
                    BufferedReader bufferedReader = new BufferedReader(inputStream);
                    string json;
                    while ((json = bufferedReader.ReadLine()) != null)
                    {
                        sb.Append(json + "\n");
                    }
                    return sb.ToString().Trim();
                }
                catch (Exception e)
                {
                    return null;
                }
            }
        }

        public void downloadJSON(string urlWebService)
        {
            DownloadJSON downloadJSON = new DownloadJSON();
            downloadJSON.Execute();
        }
    }
}

0 个答案:

没有答案