0

I have this code in java and I'm trying to port it to C#, I've already ported the majority of the code but I can't really understand how to make this two lines work in C#. I think it's a problem about how the Java code has been wrote because I have nestled two methods in Java and from what I understood it's kind of tricky to do that in C#.

If somebody could help me I would be very happy.

Sorry for my English.

Java code:

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# code:

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();
        }
    }
}
Adrian
  • 121
  • 1
  • 9
  • 1
    As I'm sure you don't, most of us don't remember errors by their number. Perhaps you could include the full error text and stack trace? – ProgrammingLlama Mar 09 '19 at 09:56
  • 1
    "to make this two lines work", which two lines? – LppEdd Mar 09 '19 at 09:57
  • Sorry, here it is: CS0120 C# An object reference is required for the non-static field, method, or property CS0103 C# The name does not exist in the current context – Adrian Mar 09 '19 at 09:57
  • There are comments above the lines that don't work, I'm sorry I couldn't make them more obvious. The errors are in the C# code. – Adrian Mar 09 '19 at 10:00
  • CS0120: You're trying to call a method that isn't part of your DownloadJSON nested class. CS0103: You're not defining `urlWebService` within the DownloadJSON nested class. – ProgrammingLlama Mar 09 '19 at 10:08
  • Thanks for your help. – Adrian Mar 09 '19 at 11:49

0 Answers0